In this example when u select from one drop down the other will populate accordingly. You can use it as it is or play with it. Enjoy !!
Index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:action name="ListingAction" executeResult="true"></s:action>
Listing.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<s:head theme="ajax" />
<title>Listing</title>
</head>
<script>
function show_details() {
dojo.event.topic.publish("show_detail");
}
</script>
<body>
<s:form id="frm_demo" name="frm_demo" theme="simple">
<table border="0">
<tr>
<td><s:select list="lstList1" name="lst"
onchange="javascript:show_details();return false;" ></s:select>
</td>
<td><s:url id="d_url" action="DetailAction" /> <s:div showLoadingText="false"
id="details" href="%{d_url}" theme="ajax"
listenTopics="show_detail" formId="frm_demo">
</s:div></td>
</tr>
</table>
</s:form>
</body>
</html>
Detail.jsp
<%@ taglib prefix="s" uri="/struts-tags"%>
<s:if test="lstList != null">
<s:select list="lstList"></s:select>
</s:if>
DetailAction.java
package ajaxdemo.action;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class DetailAction extends ActionSupport {
private String lst;
private List lstList = null;
private List lstList2 = null;
public String execute() throws Exception {
if (getLst() != null && !getLst().equals("")) {
populateDetail(getLst());
return SUCCESS;
} else {
return SUCCESS;
}
}
private void populateDetail(String id) {
lstList = new ArrayList();
if (id.equalsIgnoreCase("Fruits")) {
lstList.add("Apple");
lstList.add("PineApple");
lstList.add("Mango");
lstList.add("Banana");
lstList.add("Grapes");
} else if (id.equalsIgnoreCase("Places")) {
lstList.add("New York");
lstList.add("Sydney");
lstList.add("California");
lstList.add("Switzerland");
lstList.add("Paris");
} else {
lstList.add("Other 1");
lstList.add("Other 2");
lstList.add("Other 3");
lstList.add("Other 4");
lstList.add("Other 5");
}
}
public List getLstList() {
return lstList;
}
public void setLstList(List lstList) {
this.lstList = lstList;
}
public String getLst() {
return lst;
}
public void setLst(String lst) {
this.lst= lst;
}
}
DetailListing.java
package ajaxdemo.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.List;
public class ListingAction extends ActionSupport {
private List lstList1 = null;
public String execute() throws Exception {
populateDetail();
return SUCCESS;
}
private void populateDetail() {
lstList1 = new ArrayList();
lstList1.add("Fruits");
lstList1.add("Places");
lstList1.add("Others");
}
public List getLstList1() {
return lstList1;
}
public void setLstList1(List lstList1) {
this.lstList1 = lstList1;
}
}
Struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="demo" extends="struts-default">
<action name="ListingAction" class="ajaxdemo.action. ListingAction">
<result>/listing.jsp</result>
</action>
<action name="DetailAction" class="ajaxdemo.action. DetailAction">
<result>/detail.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>








32 comments:
Hi, I've tried your example but it doesn't work. It doesn't give an error. The list list1 is displayed but apparently the id needed to populate the second list, is null. The id of the selected value ( fruits,places and others) is set in the javascript no? For some reson it is null im my DetailAction class.
Can u help me? :(
Hi Adriana,
Thanks for your comment. Actually when I tried this example I got the same error but I found the error.
You have to replace the variable name from id to pid or any other name.
Once again thanks. I had changed my code. You can try this. If it works do write to me (It will work now :) ).
I like this example and it works for me
Thanx Vinod
Nicely done. One comment though is that the hidden field is not needed. The 'lst' parameter from the first select box is passed into the DetailAction just as the pid one is via the formId. So you can remove the hidden field and the javascript setting of the hidden field and just use get1st() after adding String 1st;.
Hi Jason
thanks for your valuable comments. You are right there is no need of hidden variable. I will be removing it from this post.
Thanks Again :)
Thank you for your post.
Appreciate it. Where do you usually refer to accomplish things like these. -harsh
Thanks for the example; it does work for me. But what if I want to populate the 2nd select based upon the listKey (integer id) of the selected value in the first select, rather than using the listValue? I've played around with this, but I can't get it to work.
Hey this is exactly what I was looking for. Good Job.
When i first load the script, i will be directed to an empty page that will be loading the second dropdown box. Only when i click on the back button of the browser, then the actual page with the two dropdown boxes will be displayed. Any idea how do i solve this problem?
Thanks for the example. Worked and was helpful.
I tried your example but strangely I get just one of the drop downs. I have javascript error that dojo is undefined.
Am I missing something?
Hey Vinod, the example is helpful, but i have a problem of my own and have got 10 solutions to it too, but not sure if they are efficient. I am writing this here cause it is related to the same example and i want to share it with the concerned masses. So, the question is if i add a submit button to your example which will invoke an action to insert the value of both the select boxes in database, and when the operation is complete, the requirement is, the user is to be returned back to the same page with state of both the select boxes maintained. I have many solutions to this and will also post it here but want to listen from you guys first.. Regards
Hi,
These days I am little busy in my office schedule. I will be very happy if you post your solution of what you have told me. But these are the comments and posting solutions here will be somewhat awkward. You can mail me the solution if you want to. I will make sure to include your name in the next post.
Thanks for your concern.. :)
Fantastic example..gr8 work...If I have a submit button in listing.jsp which maps to a method in DetailListingAction.java, how do I get the selected value for the drop down used in Detail.jsp?
Thanks in Advance
Hi Vinod,
Nice tutorial.
Similar tutorial is Struts Application using Eclipse.
Do post tutorials frequently.. Waiting to see more..
Hi
I have the same problem as the one described above: "I tried your example but strangely I get just one of the drop downs. I have javascript error that dojo is undefined."
Could you please help me with this.
Hi,
Great working Example. Only naming convention of classes/jSP/action bit difficult to understand.
Appreciate the effort.
I did make the following changes to get it work. I am using Tomcat 6.0, Struts-2.1.6 and eclipse:
1. copy struts2-dojo-plugin-2.1.6.tar from struts to WEB-INF/lib folder.
2. make changes in listing.jsp:
2.1 Add dojo taglib reference.
< %@ taglib prefix="sx" uri="/struts-dojo-tags" % >
2.2 remove the header tag < s:head theme="ajax" /> and replace it with
< sx:head debug="false" cache="false" compressed="true"/>
2.3 Change the < s:url ... action="DetailAction"/> to < s:url ... action="demo/DetailAction"/>
2.4 Change the < s:div> and < /s:div> to < sx:div> and < /sx:div>
Note: forget the space right after "<".
JSF with Struts2 example available?
Hey, here's a question that has me stumped now for a couple days and I have a feeling the answer is really easy. I am writing a struts 2 web application and for some reason, my struts 2 ajax tags (s:div specifically) only work on the welcome-file (index.jsp). When I click a link to call an action that has a result page called result.jsp, the very same s:div that executed and dispalyed its contents just fine on the welcome page no longer works on the result.jsp page. Why is this? It seems that no ajax theme elements are working on any page other than the welcome page. If you have an answer to help me, I would GREATLY appreciate it!!! :) Thanks in advance.
Thanks a lot for sharing this example.
Hi
I refer your example it is nice but I can not get both list on same page, control moves to detail page.
is there any solution for that
Thanks in advance
It is not working for me.Visiting http://localhost:8280/my-app/jsp/index.jsp gives a blank page whereas visiting http://localhost:8280/my-app/ListingAction.action gives following error:
exception
org.apache.jasper.JasperException: Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl. - Class: freemarker.core.TemplateObject
File: TemplateObject.java
Method: assertNonNull
Line: 124 - freemarker/core/TemplateObject.java:124:-1
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
root cause
Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl. - Class: freemarker.core.TemplateObject
File: TemplateObject.java
Method: assertNonNull
Line: 124 - freemarker/core/TemplateObject.java:124:-1
org.apache.struts2.components.UIBean.end(UIBean.java:515)
org.apache.struts2.views.jsp.ComponentTagSupport.doEndTag(ComponentTagSupport.java:42)
root cause
freemarker.core.InvalidReferenceException: Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl.
freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:134)
freemarker.core.BuiltIn$stringBI._getAsTemplateModel(BuiltIn.java:361)
After adding parseContent="true" to < sx:head /> , it worked by with http://localhost:8280/my-app/ListingAction.action
But accessing http://localhost:8280/my-app/jsp/index.jsp still gives blank page
Thanks for your code
I have the same above issue File: TemplateObject.java
Method: assertNonNull
freemarker.core.InvalidReferenceException: Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl.
anybody have any idea...
thanks in advance,
Rajeev
Hi,
i am getting the same excpetion
freemarker.core.InvalidReferenceException: Expression parameters.parseContent is undefined on line 45, column 28 in template/ajax/head.ftl..........
which are jars files we need to use to work these examples........?
anybody have the idea to solve this......me in trouble pls hlep.........
Thanks for this working example. Very Helpful to start.
i got a blank page?......how is it? any solutions to it
wow.. great work... keep up the good work
claudegals jr
icotp on ict
leyte
Very Nice Post.
Hi Vinod,
I tried this example.But beside first combo box I am getting the following error message.Please help me
Error loading '/struts2-blank-2.0.14/example/DetailAction.action' (404 /struts2-blank-2.0.14/example/detail.jsp)
Post a Comment