< html:select /> html tag

< html:select /> html tag

Previous Home Next

 

This struts select Tag is used to render of html <input> element of type select.We can associated with a bean property specified by our attributes.

This tag operates in two modes :-
  • multiple="true" IS NOT selected
  • multiple="true" is selected
This tag is only valid when nested inside a form tag body.

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
<html:form action="/inputAction" > 
<html:select property="country" > 
<html:option value="0">Select Country</html:option> 
<html:optionsCollection name="InputForm" 
property="countryList" label="countryName" value="countryId" /> 
</html:select> 
</html:form>

The struts select tag have following attributes:- 

  • property :- This is specifies parameter that is included withthe current request, set to the value of the selection.
  • styleId :- This is identifier to be assigned to this HTML element.
  • tabindex :- This is tab ascending positive integers order for this element.
  • title :- This attributes is the advisory title for this element.
  • titleKey :-This attributes is the advisory title for this element basically this is message resources key.
  • value :-This is used to specified the value to test for when setting the currently selected option.
  • style :- In this element you can applied CSS style.
  • styleClass :- In this element you can applied CSS stylesheet class.
  • multiple :- We can create the select list support for multiple selection.if set to true.

The Following are many other attributes which we can use :-

  • alt
  • altKey
  • disabled
  • indexed
  • onblur
  • onchange
  • onclick
  • ondblclick
  • onfocus
  • onkeydown
  • onkeypress
  • onkeyup
  • onmousedown
  • onmousemove
  • onmouseout
  • onmouseover
  • onmouseup

 


Directory Structure of SelectTagExample 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:select tag example</title>
</head>
<body>
<h3>Struts html:select tag example</h3>
<html:form action="/selectAction">
<html:messages id="errorName" property="error">
<div style="color:red"><bean:write name="errorName" /></div>
</html:messages><br/>
<bean:message key="label.name" />:
<html:select property="name">
    <html:option value="">-- None --</html:option>
	<html:option value="Mukund Singh">Mukund Singh</html:option>
	<html:option value="Sachin Tyagi">Sachin Tyagi</html:option>
	<html:option value="Somaru Ram">Somaru Ram</html:option>
	<html:option value="Rajesh Patel">Rajesh Patel</html:option>
	<html:option value="Rituraj Tyagi">Rituraj Tyagi</html:option>
	<html:option value="Amit Singh">Amit Singh</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="selectForm" type="org.r4r.struts.SelectTagForm"/>
  </form-beans>
  <action-mappings>
  <action path="/select"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/index.jsp"/>
		<action path="/selectAction" type="org.r4r.struts.SelectTagAction" 
		name="selectForm" validate="true" input="/index.jsp">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

SelectTagForm.java

package org.r4r.struts;
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;
 
@SuppressWarnings("serial")
public class SelectTagForm extends ActionForm{ 
	String name; 
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
	  HttpServletRequest request) {
 
	    ActionErrors errors = new ActionErrors();
 
	    if( getName() == null || ("".equals(getName())))
	    {
	       errors.add("error",new ActionMessage("select.required"));
	    }
 
	    return errors;
	}
 
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		name = "";
	} 
}

SelectTagAction.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 SelectTagAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
	  @SuppressWarnings("unused")
	SelectTagForm selectForm = (SelectTagForm)form;
 
	  return mapping.findForward("success");
	}
 
}

ApplicationResources.properties

# Resources for parameter 'org.r4r.struts.ApplicationResources'
# Project Struts1.3_SelectTagExample
#error message
select.required = Please select a Name.
 
#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:select tag example</title>
</head>
<body>
<h3>Struts html:select tag Example</h3>
<h4>Your selected name is :<bean:write name="selectForm" property="name" /></h4>
</body>
</html>

Output





Previous Home Next