Sei sulla pagina 1di 3

form tag - doubleselect

doubleselect : This tag is extended version of select.The two HTML select element is created
and options in second select element will be populated dynamically based on selection item from first select element.
<s:form action="AnyAction" method="post"> <s:doubleselect name="country" label="Select Country and City" list="{'Australia','India','Pakistan','USA'}" doubleName="city" doubleList="top == 'Australia'? {'Sydney', 'Melbourne','Brisbane','Perth'} :top=='India'? {'Delhi', 'Mumbai','Chennai','Kolkata'} :top=='Pakistan'? {'Karachi','Lahore','Islamabad','Rawalpindi'} :{'New York','Los Angeles','Chicago','Houston'}" /> </s:form>

Lets demostrate doubleselect tag. In this example,User will select country from first list and the major cities from selected country will be populated dynamically aftrthat click submit and get select value in Success.jsp page. Follow the steps to create this project : Step 1: Create a Dynamic Web Project in eclipse - "Struts2FormDoubleselect". Step 2: The follwing jar files required in WEB-INF\lib folder in your project. Please read Readme.txt file in the attcahed source code for more details. Step 3: Create a View page index.jsp. index.jsp:
<%@taglib uri="/struts-tags" prefix="s"%> <html> <head> <title>Form Tags</title> </head> <body> <h1>Struts2 doubleselect tag example</h1> <s:form action="DblSelectAction" method="post"> <s:doubleselect name="country" label="Select Country and City" list="{'Australia','India','Pakistan','USA'}" doubleName="city" doubleList="top == 'Australia'? {'Sydney', 'Melbourne','Brisbane','Perth'} :top=='India'? {'Delhi', 'Mumbai','Chennai','Kolkata'} :top=='Pakistan'? {'Karachi','Lahore','Islamabad','Rawalpindi'} :{'New York','Los Angeles','Chicago','Houston'}" cssClass="fieldtext" doubleCssClass="fieldtext"/> <s:submit value="Submit" /> </s:form> </body>

</html>

Step 4: Create action class DblSelectAction.java.


package techmyguru.formtags; import com.opensymphony.xwork2.ActionSupport; public class DblSelectAction extends ActionSupport { private String country,city; // getter and setter methods @Override public String execute() throws Exception { return SUCCESS; } }

Step 5: Configure Action in struts.xml file. struts.xml :


<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="devMode" value="true" /> <package name="default" extends="struts-default"> <action name="DblSelectAction" class="techmyguru.formtags.DblSelectAction"> <result>/Success.jsp</result> </action> </package> </struts>

Step 6: Create target View page Success.jsp. Success.jsp:


<%@ taglib uri="/struts-tags" prefix="s"%> <html> <body> <h2>Struts2 Doubleselect example<br><br> Your country and city is - <s:property value="country" /> <s:property value="city" /> </h2> <hr> <a href="index.jsp">Try Again</a> </body> </html>

Step 7: Verify the directory structure.

Step 8: Run the project and get output.

Enter name , click Submit button and get output.

Potrebbero piacerti anche