< html:options /> html tag

< html:options /> html tag

Previous Home Next

 

The < html:options />  tag can be used multiple times within a single <html:select> element. This 
tags is used to generate a list of HTML <option> element.

 
This tag specified in one of two modes which is depending on whether or not the collection attribute is specified.The collection attribute is specified.

<html:form action="/optionsAction">
<html:select property="name">
<html:options property="nameList" name="optionsForm"/>
</html:select>
<html:submit/>
</html:form>

The Following are attributes which we can use in this element :-

  • collection :- This attributes is specifies a  JSP bean which is itself a collection of other beans, each bean has properties named by the "property" and "labelProperty" attributes which is used  to retrieve the value and label for each option..
  • 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.
  • labelProperty  :- The property of the form bean specified by the labelName attribute, that will to retrieve the label that will be displayed to the user for this option. If the labelProperty attribute is not specified, the property named by the property attribute will be used to select both the value returned to the server.
  • 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.
  • labelName :- This attributes specifies a jsp bean  containing the collection of labels to be displayed to the user.
  • style :- In this element you can applied CSS style.
  • styleClass :- In this element you can applied CSS stylesheet class.

Directory Structure of OptionsTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>html:options tag example</title>
</head>
<body>
<h3>Struts html:options tag example</h3>
<html:form action="/optionsAction">
<bean:message key="label.name" />:
<html:select property="name">
    <html:option value="">----None----</html:option>
	<html:options property="nameList" name="optionsForm"/>
</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>

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="optionsForm" type="org.r4r.struts.OptionsTagForm"/>
  </form-beans>
  <action-mappings>
  <action path="/options" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
  <action path="/optionsAction" type="org.r4r.struts.OptionsTagAction" name="optionsForm" input="/index.jsp">	
  <forward name="success" path="/success.jsp"/>
  </action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

OptionsTagForm.java

package org.r4r.struts;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
 
@SuppressWarnings("serial")
public class OptionsTagForm extends ActionForm{ 
	String name;
	@SuppressWarnings("unchecked")
	ArrayList nameList;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList getNameList() {
		ArrayList list=new ArrayList();
		list.add("Mukund Singh");
		list.add("Amit Singh");
		list.add("Prabhat Singh");
		list.add("Praveen Singh");
		list.add("Kunal Singh");
		list.add("Gautam Singh");
		list.add("Dipu Singh");
		return list;
	}
	@SuppressWarnings("unchecked")
	public void setNameList(ArrayList nameList) {
		this.nameList = nameList;
	}
	public OptionsTagForm() {
		super();
		// TODO Auto-generated constructor stub
	}
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		name="";
	}
 }

OptionsTagAction.java

package org.r4r.struts;

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

public class OptionsTagAction extends Action{
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
	@SuppressWarnings("unused")
	OptionsTagForm optionsForm=(OptionsTagForm)form;
	  return mapping.findForward("success");
	}
}

ApplicationResources.properties

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

success.jsp

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

Output




Previous Home Next