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" %>
<sx:head />

** 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<String> cricketNations = new ArrayList<String>();
	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"%>
<sx:head />
<title>Auto complete</title>
<h3>Auto complete Dropdown | Textbox</h3>
<s:form action="displayCountry">
  <sx:autocompleter name="country" list="cricketNations" showDownArrow="false" 
     label="Cricket Nations"/>
<s:submit />


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.



<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<package name="default" extends="struts-default" namespace="/">
<action name="autoComplete" class="com.simplecode.action.AutoCompleteAction">
    <result name="success">autoComplete.jsp</result>
<action name="displayCountry" class="com.simplecode.action.AutoCompleteAction"
    <result name="success">WelcomeToCountry.jsp</result>

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


About Mohaideen Jamil

Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Pradeep Gowda

    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

  • KBL

    Example not working in UNIX server.

  • KBL

    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.

  • Nandy

    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.

  • Medha


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

  • Unknown


    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.

  • Rajdeep

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

    • Mohaideen Jamil

      Yes. 5 years ago..

      • Rajdeep

        Then should we still be using these tags..

        • Mohaideen Jamil

          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.

  • vinod

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

    • Mohaideen Jamil

      Hi Vinod,

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



  • firdose

    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

    • Mohaideen Jamil

      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.

      • firdose

        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

        • Mohaideen Jamil

          Hi Firose,

          I’m not sure weather this can be done through dojo, But you can implement with help ajax and json. Here when parent drop-down changes, call a function and make an Ajax call to your action which can return the values in JSON format, parse the JSON data and fill the other drop-down.

          I will share you code once done.

        • Mohaideen Jamil

          Hi Firose,

          Please vist the link for your queries regarding Dynamic drop down list

  • Ibrahim


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