< html:checkbox /> html tag

< html:checkbox /> html tag

Previous Home Next

 

The struts checkbox tag <html:checkbox> is a render of html <input> element of checkbox type.</html:checkbox>This Tag is populated from the specified property of the bean which is associated with our form.

 
Struts checkbox must be nested in struts form tag just like html form tag.

Note :-The underlying property value associated with this field should be of type boolean, and any value you specify should correspond to one of the Strings that indicate a true value eg . {"false", "yes", or "off"}. you can use multibox tag If you wish to utilize a set of related String values.

The struts checkbox tag have following attributes:- 

1.styleId :- This is Identifier to be assigned to this HTML element and renders an 'id' attribute.

2.tabindex :- This is tab ascending positive integers order for this element.

3.title :- This attributes is  the 
consultant
 title for this element.

4.titleKey :-This attributes is  the consultant title for this element basically this is message resources key.

5.value :-This is used when value  transmitted if this checkbox is checked when the form is submitted. if This is not specified than return 'on' value.

6.style :- In this element you can applied CSS style.

7.styleClass :- In this element you can applied CSS stylesheet class.

The Following are many other attributes which we can use :

  • accesskey 
  • alt 
  • altKey 
  • bundle 
  • dir 
  • disabled 
  • errorKey 
  • errorStyle 
  • errorStyleClass 
  • errorStyleId 
  • indexed 
  • lang 
  • name 
  • onblur 
  • onchange 
  • onclick 
  • ondblclick 
  • onfocus 
  • onkeydown 
  • onkeypress 
  • onkeyup 
  • onmousedown 
  • onmousemove 
  • onmouseout 
  • onmouseover 
  • onmouseup 
  • property 


Directory Structure of CheckboxTagExample 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><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<html:form action="/checkboxAction">
<bean:message key="label.name"/>:
<html:text property="name" name="checkboxForm"/><br/>
<bean:message key="label.technology"/>:<br/>
<html:checkbox property="technology" value="Struts 1.3" name="checkboxForm"/>
<bean:message key="label.struts1.3"/>:<br/>
<html:checkbox property="technology" value="Struts 2.0" name="checkboxForm"/>
<bean:message key="label.struts2.0"/>:<br/>
<html:checkbox property="technology" value="Hibernate 3.0" name="checkboxForm"/>
<bean:message key="label.hibernate3.0"/>:<br/>
<html:checkbox property="technology" value="Spring 2.5" name="checkboxForm"/>
<bean:message key="label.spring2.5"/><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="checkboxForm" type="org.r4r.struts.CheckboxForm">    
    </form-bean>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
        <action path="/checkbox" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
		<action path="/checkboxAction" type="org.r4r.struts.CheckboxAction" name="checkboxForm" input="index.jsp">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
</struts-config>

CheckboxForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class CheckboxForm extends ActionForm {
	private String[] technology=new String[4];
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String[] getTechnology() {
		return technology;
	}
	public void setTechnology(String[] technology) {
		this.technology = technology;
	}
}

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

ApplicationResources.properties

#label message
label.title=Struts html:checkbox tag example
label.header=Struts html:checkbox tag example
label.name=Employ Name
label.technology=Employ Technology
label.struts1.3= Struts 1.3
label.struts2.0= Struts 2.0
label.hibernate3.0= Hibernate 3.0
label.spring2.5= Spring 2.5
label.submit= Submit
label.reset= Reset

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<html>
<head>
<title><bean:message key="label.title"/></title>
</head>
<body>
<h3><bean:message key="label.header"/></h3>
<h4>OutPut:-</h4>
<h4><bean:message key="label.name"/>:<bean:write name="checkboxForm" property="name" /></h4>
<h4><bean:message key="label.technology"/>:<br/>
<logic:iterate id="multiboxItem" name="checkboxForm" property="technology">
<%= multiboxItem %><br/>
</logic:iterate></h4>
</body>
</html>

Output




Previous Home Next