In this article we will explain how to use struts2 autocompleter tag. This tag is used for getting a dropdown list with options which has at least a partial match with the text we enter in the text box. If the user clicks on the dropdown button then all options will be showing in the dropdown list..
Lets see how can we create a sample application which gives the list of planet names in the dropdown list as below.
As and when the user give input in the text field , the planet names matching with the text entered will be displaying the drop down.
Make sure that you have struts2-dojo-plugin-2.1.8.jar file in your lib folder.
Add the following code into your struts.xml file.
<!DOCTYPE struts PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 2.0//EN”
<package name=”default” extends=”struts-default”>
Create a list in the action class and populate them with planet names as shown in the “AutoCompleteAction” class.
Now let’s create ajaxDemo.jsp
In the above JSP you can notice that
1) We have included the tag library <%@ taglib prefix=”sx” uri=”/struts-dojo-tags”%>
2) <s:head theme=”ajax” /> – This is for getting the Ajax theme in our JSP page, like the background color while scrolling throgh the options etc.
3) <s:autocompleter list=”planetList” name=”planet”/>
This is for getting autocompleter list