Pages Navigation Menu

Coding is much easier than you think

Autocompleter Textbox & dropdown in Struts 2

Autocompleter Textbox & dropdown in Struts 2

To create a Autocompleter component in struts 2, please follow this two steps :
1. Add struts2-dojo-plugin.jar in your class path.
2. Include the “struts-dojo-tags” tag and its header(shown below) in your jsp page

<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

** UPDATE: Struts 2 Complete tutorial now available here.

Action class

package com.simplecode.action;

import java.util.ArrayList;
import com.opensymphony.xwork2.Action;

public class AutoCompleteAction implements Action 
	public ArrayList cricketNations = new ArrayList();
	public String country;

	public String execute() 
		return SUCCESS;

	public void populateCircketNations() 
		cricketNations.add("West Indies");
		cricketNations.add("New Zealand");
		cricketNations.add("South Africa");
		cricketNations.add("Sri Lanka");

	public String displayCountry() 
		return SUCCESS;

	public String getCountry() 
		return country;

	public void setCountry(String country) 
	{ = country;

Do read: Autocomplete in Struts 2 using Jquery and JSON via Ajax

JSP Page

Here <%@taglib uri="/struts-dojo-tags" prefix="sx"%> is a directive used in jsp for including dojo ajax tag files.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<%@taglib uri="/struts-tags" prefix="s"%>

Auto complete

Auto complete Dropdown | Textbox


Note: In <sx :autocompleter> tag, showDownArrow property indicates whether to show dropdown or not, on setting to false textbox will appear and when set to true dropdown box will appear.





Do read: AJAX implementation in Struts 2 using JQuery and JSON


On running the above example


Setting showDownArrow = “true”
autocompleter dropdown box
Setting showDownArrow = “false”
autocompleter textbox box

Zip file –
War file – AutoCompleter.war



  1. i used your display tag working nicely…. in that i have used edit delete option, delete working fine..
    how populate id to a edit page

  2. Example not working in UNIX server.

  3. Problem in running in unix environment.
    I am able to run this application in windows application. Thanks for this good example.
    I am facing trouble in running the same application on UNIX.
    ie, Adding struts2-dojo-plugin.jar in class path at UNIX.
    Any help will be appreciated.

  4. Hi,
    Your suggestions are really usefull but can you provide some examples of dynamic dropdwon in struts2. Like if we select a country, the state dropdown will be automatically populated.

  5. Hi,

    Where is autoComplete action defined in autoComplete.jsp??
    Thanx in adv.

  6. Hi,

    I need to fetch the list from DB to jsp. Now I have fetched the list but problem is, it not displaying exact value which is present in D, just printing the address of the value. How to resolve it. Please help me.

  7. I heard dojo tags were depreciated in struts 2 is it true??

    • Yes. 5 years ago..

      • Then should we still be using these tags..

        • Hi Rajdeep,

          The code for the sx tags which you are talking about where available on struts core jar.

          Latter struts 2 developers provided an alternate way , to use dojo tag by providing struts2-dojo-plugin-2.x.x.x plugin for this purpose.

          Here in this above example we are using this plugin to implement this example.

          These plugin are included in latest release of struts 2, You can verify the same.

  8. nothing i sgoign on i have aded the jar file in project no autocomplete is happening

    • Hi Vinod,

      This program works fine, may you have missed some configuration, I will upload the war file and will let you know.



  9. hi iam using dojo tags in struts iam getting an error struts-dojo-tags file not,even though i added all related jars please help me

    • Hi firdose,

      Sorry for delay,

      In case of struts 2 normally the jar added via Java Build Path wont get configured in s2 application, so please copy and paste the jar in lib folder, then clean,build and restart the application.

      If you problem persists then let me know , I will mail you the war file.

      • shukriya mohaideen i got it,but iam facing one more problem,i.e about dependency dropdown list ,i have two dropdown list second is based on first,suppose if i select India in first dropdownlist it related shates have to populate second dropdown list,values have to come from distance could u please help me.thanks in advancd

  10. Hi,

    Thank you for creating this tutorial. It’s very clearly written.

Leave a Comment

Your email address will not be published. Required fields are marked *