Home Struts Struts Struts2.2.1 Tags Struts2.2.1 optgroup example 

Ask Questions?

View Latest Questions


 
 

Struts2.2.1 optgroup example 
Posted on: January 13, 2011 at 12:00 AM
Advertisement
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>

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

<body>

<h1>StrutsOptGroup_Tag_Example</h1><hr>

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

</body>

</html>

2-optGroupJsp.jsp

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

<html>

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

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

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

<s:select label="Select City"

name="Cites"

list="nameofCity">

<s:optgroup label="City_of_UP"

list=" upCitiesName" />

<s:optgroup label="City_Of_UttraKhand"

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

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

</s:form>

</body>

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

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

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

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

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

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

</action></package>

</struts>

5_WelcomePage.jsp

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

<head><title>StrutsOptGroup_Tag_Example</title>

</head>

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

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

</body></html>

indexJsp.gif

OptionGroupt.gif

SelectOption.gif

SelecteValue.gif

Download Source Code

Related Tags for Struts2.2.1 optgroup example :


DMCA.com