< html:optionsCollection /> html tag

< html:optionsCollection /> html tag

Previous Home Next

 

The <html:optionCollection /> tag is a render of HTML <option> element of type representing possible choices for a <select> element.This tag can be used multiple times within a single <html:select> element.This tag operates on a collection of beans, where each bean has a label property and a value property.

 
This tags makes more consistent use of the name and property attributes.

Note:- This tag does not support a styleId attribute because this have more than one id element might have the same value, which the HTML specification says is illegal.

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
< html:form action="/inputAction"> 
<html:select property="state" > 
<html:option value="0">Select State</html:option> 
<html:optionsCollection name="InputForm" 
property="stateList" label="label" value="value" /> 
</html:select> 
<html:submit property="method" value="save" /> 
</html:form>

The Following are attributes which we can use in this element :-
  • property :- The property of the form bean specified by the name attribute, that will retrieve the value that will be returned to the server if this option is selected.
  • filter :- By default, This attributes set is true  but you can set to false if you do not want the option labels filtered for sensitive characters in HTML.
  • label :- This represent the current object in the collection the represent sthe <option> label. 
  • style :- In this element you can applied CSS style.
  • styleClass :- In this element you can applied CSS stylesheet class.
  • value :- This represent the current object in the collection the represent sthe <option> value. 

Directory Structure of OptionsCollectionTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="optionAction.do?method=myOption"/>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name> 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans >
   <form-bean name="optionForm" type="org.r4r.struts.OptionsCollectionForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
   <action path="/optionAction" scope="session" name="optionForm" 
   type="org.r4r.struts.OptionsCollectionAction" input="/input.jsp" parameter="method">
   <forward name="success" path="/input.jsp"/>
   </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

OptionsCollectionTagForm.java

package org.r4r.struts;

import java.util.ArrayList;
import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class OptionsCollectionForm extends ActionForm {
	private String studentName;
	@SuppressWarnings("unchecked")
	private ArrayList studentList;
	public OptionsCollectionForm() {
		super();
		// TODO Auto-generated constructor stub
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	@SuppressWarnings("unchecked")
	public ArrayList getStudentList() {
		return studentList;
	}
	@SuppressWarnings("unchecked")
	public void setStudentList(ArrayList studentList) {
		this.studentList = studentList;
	}
}

OptionsCollectionTagAction.java

package org.r4r.struts;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

public class OptionsCollectionAction extends DispatchAction {
	@SuppressWarnings("unchecked")
	public ActionForward myOption(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response)throws Exception{
		ArrayList studentList=new ArrayList();
		OptionsCollectionForm optionForm=(OptionsCollectionForm)form;
		studentList.add(new OptionsCollection("1","Mukund Singh"));
		studentList.add(new OptionsCollection("2","Amit Singh"));
		studentList.add(new OptionsCollection("3","Prabhat Singh"));
		studentList.add(new OptionsCollection("4","Praveen Singh"));
		studentList.add(new OptionsCollection("5","Kunal Singh"));
		studentList.add(new OptionsCollection("6","Gautam Singh"));
		optionForm.setStudentList(studentList);
		return mapping.findForward("success");
	}

}

OptionsCollection.java

package org.r4r.struts;

public class OptionsCollection{
	private String studentId;
	private String studentName;
	public OptionsCollection(String studentId, String studentName) {
		super();
		this.studentId = studentId;
		this.studentName = studentName;
	}
	public String getStudentId() {
		return studentId;
	}
	public void setStudentId(String studentId) {
		this.studentId = studentId;
	}
	public String getStudentName() {
		return studentName;
	}
	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_OptionsCollectionTagExample
#label message
label.name=Select a Name
label.submit=Submit
label.reset=Reset

input.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
<title>Struts html:optionsCollection tag example</title>
</head>
<body>
<h3>Struts html:optionsCollection tag example</h3>
<html:form action="/optionAction">
<bean:message key="label.name"/>:
<html:select property="studentName">
<html:option value="">---None---</html:option>
<html:optionsCollection property="studentList" value="studentId" label="studentName" name="optionForm"/>
</html:select>
<br/>
<br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form>
</body>
</html>

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>Struts html:optionsCollection tag example</title>
</head>
<body>
<h3>Struts html:optionsCollection tag Example</h3>
<h4>Your selected Name is :
<bean:write name="optionForm" property="studentName" />
</h4>
</body>
</html>

Output




Previous Home Next