Struts2.2.1 optgroup example 
Posted on: January 13, 2011 at 12:00 AM
In this section you will see the use of struts optgroup tags.

Struts2.2.1 optgroup example 

In this tutorial, you will see the use of optgroup tag of struts2.2.1. The optgroup tag is a generic Form_UI tag that creates an optgroup component which needs to reside within a select tag <s:select>.

Directory structure of optgroup tag example.

 1- index.jsp

<html>ADS_TO_REPLACE_1

<head><title>StrutsOptGroup_Tag_Example</title></head>

<body>

<h1>StrutsOptGroup_Tag_Example</h1><hr>ADS_TO_REPLACE_2

<a href="OptGroupAction.action">OptGroupAction</a>

</body>

</html>

2-optGroupJsp.jsp ADS_TO_REPLACE_3

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

<html>

<head><title>StrutsOptGroup_Tag_Example</title></head>ADS_TO_REPLACE_4

<body><h1>StrutsOptGroup_Tag_Example</h1><hr>

<s:form action="ResultOptGroup.action">

<s:select label="Select City"ADS_TO_REPLACE_5

name="Cites"

list="nameofCity">

<s:optgroup label="City_of_UP"ADS_TO_REPLACE_6

list=" upCitiesName" />

<s:optgroup label="City_Of_UttraKhand"

list="%{#{'Rudrapur':'Rudrapur','RamNagar':'RamNagar','Doon':'Doon'}}" />ADS_TO_REPLACE_7

</s:select><s:submit></s:submit>

</s:form>

</body>ADS_TO_REPLACE_8

</html>

3->OptGroupAction.java

package roseindia.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class OptGroupAction extends ActionSupport {

private List<String> nameofCity;
private Map<String,String> upCitiesName;
private String Cites;

public List<String> getNameofCity() {
return nameofCity;
}
public void setNameofCity(List<String> nameofCity) {
this.nameofCity = nameofCity;
}
public Map<String, String> getUpCitiesName() {
return upCitiesName;
}
public void setUpCitiesName(Map<String, String> upCitiesName) {
this.upCitiesName = upCitiesName;
}
public String getCites() {
return Cites;
}
public void setCites(String cites) {
Cites = cites;
}
public OptGroupAction()
{
nameofCity=new ArrayList<String>();
nameofCity.add("Mumbai");
nameofCity.add("Bengaluru");
nameofCity.add("Indore");
nameofCity.add("Delhi");

upCitiesName=new HashMap<String, String>();
upCitiesName.put("Lucknow", "Lucknow");
upCitiesName.put("Bareilly","Bareilly");
upCitiesName.put("Allahabad","Allahabad");
upCitiesName.put("Rampur","Rampur");

}
public String execute() { 
return SUCCESS;}

}

4_struts.xml

<struts>

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

<constant name="struts.devMode" value="false" />ADS_TO_REPLACE_9

<package name="roseindia" extends="struts-default" namespace="/">

<action name="OptGroupAction" class="roseindia.action.OptGroupAction">

<result >/optGroupJsp.jsp</result> </action>ADS_TO_REPLACE_10

<action name="ResultOptGroup" class="roseindia.action.OptGroupAction">

<result name="success" >/selectedValue.jsp</result>

</action></package>ADS_TO_REPLACE_11

</struts>

5_WelcomePage.jsp

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

<head><title>StrutsOptGroup_Tag_Example</title>

</head>ADS_TO_REPLACE_12

<body><h1>StrutsOptGroup_Tag_Example</h1><hr>

Selected city : <s:property value="Cites"/>

</body></html>

indexJsp.gifADS_TO_REPLACE_13

OptionGroupt.gif

ADS_TO_REPLACE_14

SelectOption.gif

SelecteValue.gifADS_TO_REPLACE_15

Download Source Code

Related Tags for Struts2.2.1 optgroup example :

Advertisements

Ads

Ads

 
Advertisement null

Ads