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 EasyAutocomplete Jquery Autocomplete Plugin website it is for Free.
The code is Written below.
In order to use EasyAutocomplete, add these files in the header section of your page.
<!-- JS file --> <script src="path/to/jquery.easy-autocomplete.min.js"/> <!-- CSS file --> <link rel="stylesheet" href="path/to/easy-autocomplete.min.css"/> <!--Your Jquery min.js --> <script src="Your/path/jquery-1.11.2.min.js"/>
After adding this you have to add code in jsp.
This is jsp code
Hello World!
<script type="text/javascript"> var options = { url: "easyAutocomplete", <!-this is your action name--> listLocation: "model", <!-this your bean Object--> getValue: "name", <!-Display Object variable name--> template: { type: "description", fields: {description: "username"}}, list: {match: {enabled: true } } }; $("#basics").easyAutocomplete(options); </script>
After Adding This You have to Add mapping in Struts.xml. Here I am Using struts2 XML for mapping you can used Mapping or Annotation.
This is struts.xml code which return Json as response .
This is Bean Class or Model Class which contain getter and setter
![]() |
Java Bean class |
This is SturtsAction class which contains easyAutocomplete Method. In this Method, I have Set Static Values in Model Class Object variable you can set by a call to Your Database.
public String easyAutocomplete() { try { model = new ArrayList(); ModelClass m1 = new ModelClass(); m1.setName("Ziyad"); m1.setUsername("Zee"); m1.setPassword("123"); m1.setPhone(145623546); ModelClass m2 = new ModelClass(); m2.setName("Zaid"); m2.setUsername("Zaa"); m2.setPassword("456"); m2.setPhone(64578912); ModelClass m3 = new ModelClass(); m3.setName("Humera"); m3.setUsername("hum"); m3.setPassword("987"); m3.setPhone(456123); ModelClass m4 = new ModelClass(); m4.setName("Rocknroll"); m4.setUsername("Rock"); m4.setPassword("852"); m4.setPhone(45236978); model.add(m1); model.add(m2); model.add(m3); model.add(m4); model.add(m4); } catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
Result Output:
![]() |
Output |