Autocomplete In Java Solution, is a feature in which an application predicts the rest of a word a user is typing. Its help them to get Response Faster and easy way. Autocomplete original purpose of word prediction software was to help people in increasing their typing speed, as well as to help them decrease the number of keystrokes needed in order to complete a word or a sentence.


This Autocomplete is written in Struts2, you can use this Autocomplete in anywhere in your Application.
This Autocomplete has Derived from this Select2  Autocomplete Plugin website it is for Free.
Select2  gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

In order to use Select2, add these files in the header section of your page.Select2  CDN files below
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css"
 rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js">
</script>
</head>
After adding this Select2 CND file you have to add below code to your HMTL here I am using jsp.
This is JSP code.
<body>


<script type="text/javascript">

$(document).ready(function () {
     $('.js-example-basic-single').select2({
			placeholder: 'Select an item',
				ajax: {
				url: 'fetchEmployeeSuggestion',
				dataType: 'json',
				data: function (params) {
				var query = {
				search: params.term,     //this is Sending Input key by search
				userInput: params.term  //this is Sending Input key by userInput
			}
			return query;
		},
			processResults: function (data) { //Return Response
			console.log(data);
			var results = $.map(data.employee, function (obj) {
		
				return {
						id: obj.empCode,
						text: obj.empName + " [" + obj.designation + "]"
					};
				});
				console.log(results);   //Printing Response
				return {
			results: results,
			};
		},
			"more": true,
			cache: true
		}
				});
			});
</script>
</body>

This is Bean Class or Model Class which contain getter and setter
public class Employee {

		public String empCode;
		public String empName;
		public String designation;
		public String locCode;
		public String locName;
		public String psaCode;
		public String psa;
		public String locType;
		public String locTypeDesc;
		public String designationLong;
		public String divisionCd;

		// getter setter\\\
	}

This is struts.xml code

 


This is Action or Controller class which contains fetchEmployeeSuggestion Method. In this Method, we are fetching userInput of TextField which we type in jsp.
Then we are getting Data From Database by calling by findEmployeeSuggestion  of Dao Class which is returning  Bean Class Object by setting value from a Database call. 
public class EmployeeAction extends ActionSupport {
		ArrayList<Employee> employee;
		String userInput;
		// getter setter\\\
		public String findMatchingEmployee() {
			EmployeeDAO empDao = new EmployeeDAO();
			employee = empDao.findEmployeeSuggestion(userInput);
			return "success";
		}
	}

This is DaoClass, in this Class we are return  ArrayList<Employee> as a response to Controller class
public ArrayList<Employee> findEmployeeSuggestion(String userInput) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Employee> empList = null;
 try {
con = ConnectionManager.getConnectionFromPool();
 String employeeSuggestion = "SELECT EMPDB.EMP_CODE,EMPDB.EMP_NAME,EMPDB.DESIGN_SHORT_DESC FROM Employee EMPDB ";
            try {
                pstmt = con.prepareStatement(employeeSuggestion);
                empList = new ArrayList<Employee>();
                try {
                    Employee emp = null;
                    rs = pstmt.executeQuery();
                    while (rs.next()) {
                    Employee emp=new Employee();
                    emp.setEmpCode(rs.getString("EMP_CODE"));
                    emp.setEmpName(rs.getString("EMP_NAME"));
                    emp.setDesignation(rs.getString("DESIGN_SHORT_DESC"));
                        empList.add(emp);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e);
                } finally {
                    if (rs != null) {
                        rs.close();
                        rs = null;
                    }
                }
            } catch (Exception e) {
                log.error(e);
            } finally {
                if (pstmt != null) {
                    pstmt.close();
                    pstmt = null;
                }
            }
        } catch (Exception e) {
            log.error(e);
        } finally {
            try {
                if (con != null) {
                    ConnectionManager.closeConnection(con);
                    con = null;
                }
            } catch (Exception e) {
            }
        }
        return empList;
    }
This ScreenShot of Result Output
OUTPUT
JSON RETURN

Post a Comment

أحدث أقدم