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
AutoCompleteAction.java
package com.simplecode.action; import java.util.ArrayList; import com.opensymphony.xwork2.Action; public class AutoCompleteAction implements Action { public ArrayListcricketNations = new ArrayList (); 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"%>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.
struts.xml
autoComplete.jsp WelcomeToCountry.jsp
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”
Setting showDownArrow = “false”
|
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
Example not working in UNIX server.
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.
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
Refer https://www.simplecodestuffs.com/ajax-implementation-in-struts-2-using-jquery-and-json/
Hi,
Where is autoComplete action defined in autoComplete.jsp??
Thanx in adv.
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.
Please mail me your code to [email protected]
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.
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.
Thanks
Jamil
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
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.
Hi Firose,
Please vist the link https://www.simplecodestuffs.com/ajax-implementation-in-struts-2-using-jquery-and-json/ for your queries regarding Dynamic drop down list
Hi,
Thank you for creating this tutorial. It’s very clearly written.