jdeveloper

Struts 2 is a powerful web application development framework. Its built on top of WebWork framework and rebranded as Struts 2. Due to excessive popularity of Struts 1 its difficult to search for much relevant documentation on web about struts2. Slowly the search engines are improving on that.

Struts-2-Developers: Struts 2 - AJAX Drop down Example

Search Web......

Struts 2 - AJAX Drop down Example


Struts2 AJAX Drop down Example, struts2 ajax auto populate drop down, struts2 ajax auto select drop down, Struts2 AJAX Example, Struts2 AJAX Drop down sample Code, Struts 2, beginners example for struts2, struts2 simple application,struts2 with ajax

Struts 2 is a powerful web application development framework. Its built on top of WebWork framework and rebranded as Struts 2. Due to excessive popularity of Struts 1 its difficult to search for much relevant documentation on web about struts2. Slowly the search engines are improving on that.

In this example when you select from one drop down the other will populate accordingly. You can use it as is or play around with it based on your need

Index.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>



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"%>






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


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">



/listing.jsp


/detail.jsp





web.xml




struts2
org.apache.struts2.dispatcher.FilterDispatcher


struts2
/*


index.jsp


12 Post Comment:

Priyanka said...

Really useful examples....do u have some code snippet for DOJO AJAX calls...

Kavita Pathak said...

Hi,
I tried it but it goes into infinite loop and finally the browser hangs :(

Kavita Pathak said...

As Ajax UI tags were moved to the new dojo plugin, Use uri="/struts-dojo-tags" instead of uri="/struts-tags"

Software Wikipedia said...

@Kavita thanks for your feedback, I will update the code. This may be due to the difference in Struts 2 versions we are using. I hope its working for you now.

Let me know if there is any other issue you are facing.

Eswar said...

Very nice tutorial.

regards,
Eswar.
Vaannila

joel said...

how to run the program

lalitha said...

hey with out Ajax can ve create dropdown list in struts2??
Is it possible??
If s,tell me the code plss???

Software Wikipedia said...

Yes, you can always create a list without using AJAX. The first list in this example does not require AJAX call. This example demonstrates how you can get the values in another drop down when one option is selected. Let me know what exactly you are looking for?

Software Wikipedia said...

@joel Struts2 is a web based application framework and requires you to have a JEE server like Tomcat or Glassfish. you can download it from web and deploy your web application. You may want to read more about the JEE fundamentals before you get into this framework.

Anonymous said...

I am getting error like dojo is not defined. Can anybody help ?

Software Wikipedia said...

@Anonymous -

1. can you post the exact error from the logs please.
2. Which version of Struts2 are you using?

Let me know and I will try to resolve the issue for you.

Software Wikipedia said...

Do you have struts2-dojo-plugin-*.jar in you app/WEB-INF/lib directory? For dojo plugin to work this jar needs to be in your application CLASSPATH.

Post a Comment

 
Make a Free Website with Yola.