<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
Code language: HTML, XML (xml)
First define the taglib sx which we will use to add AJAX enabled tags. <sx:head/>
Code language: HTML, XML (xml)
Add this head tag in your JSP between <head> … </head> tags. This sx:head tag will include required javascript and css files to implement Ajax. AjaxAutocomplete.java
in net.viralpatel.struts2
package and copy following content into it. AjaxAutocomplete.java package net.viralpatel.struts2;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import com.opensymphony.xwork2.ActionSupport;
public class AjaxAutocomplete extends ActionSupport {
private String data = "Afghanistan, Zimbabwe, India, United States, Germany, China, Israel";
private List<String> countries;
private String country;
public String execute() {
countries = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(data, ",");
while (st.hasMoreTokens()) {
countries.add(st.nextToken().trim());
}
return SUCCESS;
}
public String getCountry() {
return this.country;
}
public List<String> getCountries() {
return countries;
}
public void setCountries(List<String> countries) {
this.countries = countries;
}
public void setCountry(String country) {
this.country = country;
}
}
Code language: Java (java)
In above code we have created a simple action class with attribute String country
and List countries
. The countries list will be populated with country names when execute() method is called. Here for this example, we have loaded static data. You may feel free to change this and add data from database. <%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Welcome</title>
<sx:head />
</head>
<body>
<h2>Struts 2 Autocomplete (Drop down) Example!</h2>
Country:
<sx:autocompleter size="1" list="countries" name="country"></sx:autocompleter>
</action>
</body>
</html>
Code language: HTML, XML (xml)
In above JSP file we have used sx:autocompleter tag to render an autocomplete drop down which users Ajax class to fetch data internally. Note that we have mapped the list
attribute with List countries
. <action name="ajaxdemo" class="net.viralpatel.struts2.AjaxAutocomplete">
<interceptor-ref name="loggingStack"></interceptor-ref>
<result name="success" type="tiles">/ajaxdemo.tiles</result>
<result type="tiles">/ajaxdemo.tiles</result>
</action>
Code language: HTML, XML (xml)
Notice that we are using Tiles here in this example. You may want to use AjaxDemo.jsp instead of /ajaxdemo.tiles to render the output directly in JSP. Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
hi..
I tried the helloworld appin part 2..donno what happened, but it wasnt working at all..and 2-3 days I played around with it with no luck..today I just saw that this app has exactly the jar files I have and i just tried, luckily its working..phew!!
Now I need to go in detail to the specifics..to see what happened wrong last time..mostly some jar file mismathc i guess..
Anyways thanks..
hi
Hi-
I have tried the above example and it is not working for me, may be I am missing something which I was unable to figure out. Here the error log that I am getting:
I have included all the dependency jars, still I am getting the below error. There are two jars in my project library which has the class ValueStack, xwork-2.1.2.jar & xwork-core-2.1.5.jar. I even tried keeping one at a time, still getting the same error. Please help me to resolve this issue.
Thanks in advance.
org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue(Ljava/lang/String;Z)Ljava/lang/Object;
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:527)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:401)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
I have the same problem :(, help:
java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.ValueStack.findValue(Ljava/lang/String;Ljava/lang/Class;Z)Ljava/lang/Object;
at org.apache.struts2.components.Component.findValue(Component.java:382)
at org.apache.struts2.components.UIBean.evaluateParams(UIBean.java:668)
at org.apache.struts2.components.UIBean.end(UIBean.java:510)
at org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
at org.apache.jsp.pages.choice1_jsp._jspx_meth_s_005ftextfield_005f0(choice1_jsp.java:290)
at org.apache.jsp.pages.choice1_jsp._jspx_meth_s_005fform_005f0(choice1_jsp.java:144)
Don't know what's the problem... Can I have some help ?
Thanks,
Correction:
You need to be more explicit how to get this to run. A couple of things that should be added to the tutorial:
1) Code for the tiles.xml file, simialr to the following:
2) Instructions telling the user to use “ajaxdemo” in the URL when running the application: http://localhost:8080/StrutsTutorial/ajaxdemo.
If you try to run it given only the instructions here, you end up getting a 404 or 500 http error.
the dropdown list is not working
If you add the same library(jar) files it works fine.
Hi Viral, Excellent Job. I have tried all the Struts2 tutorials. Thanks for the cool tutorials
hi Anu,
what jars did you add?
I have tried the above example the customer add and file upload modules modules are working properly.
The autocomplete ajax is not fetching the array list .. anyone has any clue
Furqan, i'm having the same issue. I'm guessing it has something to do with a lack of a method="execute" in AjaxDemo.jsp but i'm not sure.