Ajax drop down menu tutorial
Struts 2 has emerged as boon for developers. But the documentation available is very small. So I had decided to give a brief demonstration of the ajax used in struts 2.
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>