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

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

Action class

 
AutoCompleteAction.java
 

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() 
	{
		populateCircketNations();
		return SUCCESS;
	}

	public void populateCircketNations() 
	{
		cricketNations.add("Australia");
		cricketNations.add("England");
		cricketNations.add("India");
		cricketNations.add("West Indies");
		cricketNations.add("New Zealand");
		cricketNations.add("Pakistan");
		cricketNations.add("Bangladesh");
		cricketNations.add("South Africa");
		cricketNations.add("Sri Lanka");
		cricketNations.add("Zimbabwe");
	}

	public String displayCountry() 
	{
		return SUCCESS;
	}

	public String getCountry() 
	{
		return country;
	}

	public void setCountry(String country) 
	{
		this.country = country;
	}	
}

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

JSP Page

 
autoComplete.jsp
 
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"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="/struts-dojo-tags" prefix="sx"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<sx:head />
<head>
<title>Auto complete</title>
</head>
<body>
<h3>Auto complete Dropdown | Textbox</h3>
<s:form action="displayCountry">
  <sx:autocompleter name="country" list="cricketNations" showDownArrow="false" 
     label="Cricket Nations"/>
<s:submit />
</s:form>
</body>
</html>

 

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.

struts.xml

 

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

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

Demo

 
On running the above example

http://localhost:8089/AutoComplete/autoComplete.action

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

dwd2
Zip file – AutoCompleter.zip
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.
    Hi,
    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

    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.
    Thanks

  • Medha

    Hi,

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

  • Unknown

    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.

  • Rajdeep

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

    • http://www.simplecodestuffs.com Mohaideen Jamil

      Yes. 5 years ago..

      • Rajdeep

        Then should we still be using these tags..

        • http://www.simplecodestuffs.com 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

    • http://www.simplecodestuffs.com 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.

      Thanks

      Jamil

  • 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

    • http://www.simplecodestuffs.com 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

        • http://www.simplecodestuffs.com 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.

        • http://gravatar.com/jamilrehman Mohaideen Jamil

          Hi Firose,

          Please vist the link http://www.simplecodestuffs.com/ajax-implementation-in-struts-2-using-jquery-and-json/ for your queries regarding Dynamic drop down list

  • Ibrahim

    Hi,

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