< html:option /> html tag
Previous | Home | Next |
This struts option tag is a render of HTML <input> element of type option which represent a single option nested a <select> element.The value of the corresponding bean property matches the specified value, this option will be marked selected. This tag is only valid when nested inside a <html:select> tag body.
Example :-<%@ 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:select> <html:submit property="method" value="save" /> </html:form>The struts option tag have following attributes:-
- styleId :- This is identifier to be assigned to this HTML element.
- style :- In this element you can applied CSS style.
- styleClass :- In this element you can applied CSS stylesheet class.
- value* :-This is represent the value that is submitted when checkbox is selected by user.
- bundle :- This attributes specifies a MessageResources key of the resource bundle defined in the struts-config element .
- disabled :- This is set to true means HTML element to be disabled.
- key :- This is defined the Resources key.
- locale :-This is specifies teh session attributes\ containing the Locale instance of the current request.
Directory Structure of OptionTagExample 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:option tag example</title> </head> <body> <h3>Struts html:option tag example</h3> <html:form action="/optionAction"> <div> <html:messages id="errorDate" property="dateError"> <div style="color:red"><bean:write name="errorDate" /></div> </html:messages><br/> <html:messages id="errorMonth" property="monthError"> <div style="color:red"><bean:write name="errorMonth" /></div> </html:messages><br/> <html:messages id="errorYear" property="yearError"> <div style="color:red"><bean:write name="errorYear" /></div> </html:messages></div><br/> <bean:message key="label.date" />: <html:select property="date"> <html:option value="">--None--</html:option> <html:option value="1">1</html:option> <html:option value="2">2</html:option> <html:option value="3">3</html:option> <html:option value="4">4</html:option> <html:option value="5">5</html:option> <html:option value="6">6</html:option> <html:option value="7">7</html:option> <html:option value="8">8</html:option> <html:option value="9">9</html:option> <html:option value="10">10</html:option> <html:option value="11">11</html:option> <html:option value="12">12</html:option> <html:option value="13">13</html:option> <html:option value="14">14</html:option> <html:option value="15">15</html:option> <html:option value="16">16</html:option> <html:option value="17">17</html:option> <html:option value="18">18</html:option> <html:option value="19">19</html:option> <html:option value="20">20</html:option> <html:option value="21">21</html:option> <html:option value="22">22</html:option> <html:option value="23">23</html:option> <html:option value="24">24</html:option> <html:option value="25">25</html:option> <html:option value="26">26</html:option> <html:option value="27">27</html:option> <html:option value="28">28</html:option> <html:option value="29">29</html:option> <html:option value="30">30</html:option> <html:option value="31">31</html:option> </html:select> <bean:message key="label.month" />: <html:select property="month"> <html:option value="">--None--</html:option> <html:option value="January">January</html:option> <html:option value="February">February</html:option> <html:option value="March">March</html:option> <html:option value="April">April</html:option> <html:option value="May">May</html:option> <html:option value="June">June</html:option> <html:option value="July">July</html:option> <html:option value="August">August</html:option> <html:option value="September">September</html:option> <html:option value="October">October</html:option> <html:option value="November">November</html:option> <html:option value="December">December</html:option> </html:select> <bean:message key="label.year" />: <html:select property="year"> <html:option value="">-- None --</html:option> <html:option value="2008">2008</html:option> <html:option value="2009">2009</html:option> <html:option value="2010">2010</html:option> <html:option value="2011">2011</html:option> <html:option value="2012">2012</html:option> <html:option value="2013">2013</html:option> </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="optionForm" type="org.r4r.struts.OptionTagForm"/> </form-beans> <action-mappings> <action path="/option" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/> <action path="/optionAction" type="org.r4r.struts.OptionTagAction" name="optionForm" validate="true" input="/index.jsp"> <forward name="success" path="/success.jsp"/> </action> </action-mappings> <message-resources parameter="org.r4r.struts.ApplicationResources" /> </struts-config>OptionTagForm.javapackage org.r4r.struts; import java.util.Iterator; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionMessage; import org.apache.struts.upload.CommonsMultipartRequestHandler; import org.apache.struts.upload.MultipartRequestWrapper; @SuppressWarnings("serial") public class OptionTagForm extends ActionForm{ String date; String month; String year; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getMonth() { return month; } public void setMonth(String month) { this.month = month; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } @Override public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if(( getDate() == null || ("".equals(getDate())))&&( getMonth() == null || ("".equals(getMonth())))&&( getYear() == null || ("".equals(getYear())))) { errors.add("dateError",new ActionMessage("date.required")); } if( getMonth() == null || ("".equals(getMonth()))) { errors.add("monthError",new ActionMessage("month.required")); } if( getYear() == null || ("".equals(getYear()))) { errors.add("yearError",new ActionMessage("year.required")); } return errors; } @SuppressWarnings("unchecked") public void reset(ActionMapping mapping, HttpServletRequest request) { // reset properties if (request instanceof MultipartRequestWrapper) { MultipartRequestWrapper wrapper = (MultipartRequestWrapper) request; CommonsMultipartRequestHandler handler = new CommonsMultipartRequestHandler(); handler.setMapping(mapping); try { handler.handleRequest(request); } catch (ServletException ex) { System.out.println(ex); } Map paramMap = handler.getTextElements(); for (Iterator it = paramMap.keySet().iterator(); it.hasNext() ;) { String key = (String) it.next(); String value = (String) paramMap.get(key); wrapper.setParameter(key, value); } } } }OptionTagAction.javapackage 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 OptionTagAction extends Action{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { @SuppressWarnings("unused") OptionTagForm selectForm = (OptionTagForm)form; return mapping.findForward("success"); } }ApplicationResources.properties# Resources for parameter 'org.r4r.struts.ApplicationResources' # Project Struts1.3_SelectTagExample #error message date.required = Please select a Date. month.required = Please select a Month. year.required = Please select a Year. #label message label.date = Select a Date label.month = Select a Month label.year = Select a Year label.submit = Submit label.reset = Resetsuccess.jsp<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <html> <head> <title>Struts html:select tag example</title> </head> <body> <h3>Struts html:select tag Example</h3> <h4>Your selected Date/Month/Year is : <bean:write name="optionForm" property="date" />/ <bean:write name="optionForm" property="month" />/ <bean:write name="optionForm" property="year" /> </h4> </body> </html>Output
Previous | Home | Next |