Struts <html:checkbox> checkbox example

Download this Struts check box example – Struts-CheckBox-Example.zip

In this Struts example, you will learn how to create a HTML check box input field with Struts <html:checkbox> tag

1. Folder Structure

This is the final project structure created by Maven. Please create the corresponding folders.

2. Action class

Create an Action class, do nothing but forward the request.

HtmlCheckBoxAction.java

package com.mkyong.common.action;
 
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 HtmlCheckBoxAction extends Action{
 
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) 
        throws Exception {
 
		return mapping.findForward("success");
	}
 
}

3. Properties file

Create a properties file, and declare the error and label messages.

Common.properties

#error message
error.common.html.checkbox.required = Please tick the checkbox.
 
#label message
label.common.html.checkbox.name = CheckBox
label.common.html.checkbox.button.submit = Submit
label.common.html.checkbox.button.reset = Reset

4. ActionForm

Create a ActionForm, accept a checkbox value.

HtmlCheckBoxForm.java

package com.mkyong.common.form;
 
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;
 
public class HtmlCheckBoxForm extends ActionForm{
 
	String checkboxValue;
 
	public String getCheckboxValue() {
		return checkboxValue;
	}
 
	public void setCheckboxValue(String checkboxValue) {
		this.checkboxValue = checkboxValue;
	}
 
	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
 
	    ActionErrors errors = new ActionErrors();
 
	    if( getCheckboxValue() == null || ("".equals(getCheckboxValue()))) {
	       errors.add("common.checkbox.err",
	    	  new ActionMessage("error.common.html.checkbox.required"));
	    }
 
	    return errors;
	}
 
	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// reset properties
		checkboxValue = "";
	}
 
}

5. JSP Page

Use the Struts’s HTML tag <html:checkbox> to create a HTML checkbox input field.

checkbox.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
</head>
<body>
<h1>Struts html:checkbox example</h1>
 
<html:form action="/CheckBox">
 
<html:messages id="err_name" property="common.checkbox.err">
<div style="color:red">
	<bean:write name="err_name" />
</div>
</html:messages>
 
<div style="padding:16px">
<bean:message key="label.common.html.checkbox.name" /> :
	<html:checkbox property="checkboxValue" />
</div>
 
<div style="padding:16px">
	<div style="float:left;padding-right:8px;">
		<html:submit>
                  <bean:message key="label.common.html.checkbox.button.submit" />
                </html:submit>
	</div>
	<html:reset>
           <bean:message key="label.common.html.checkbox.button.reset" />
        </html:reset>
</div>
 
</html:form>
 
</body>
</html>

Display the check box value.

display.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
</head>
<body>
<h1>
CheckBox value : <bean:write name="htmlCheckBoxForm" property="checkboxValue" />
</h1>
</body>
</html>

6. struts-config.xml

Create a Struts configuration file and link all together.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
 
<struts-config>
 
	<form-beans>
		<form-bean
			name="htmlCheckBoxForm"
			type="com.mkyong.common.form.HtmlCheckBoxForm"/>
 
	</form-beans>
 
	<action-mappings>
 
	        <action
			path="/CheckBoxPage"
			type="org.apache.struts.actions.ForwardAction"
			parameter="/pages/checkbox.jsp"/>
 
		<action
			path="/CheckBox"
			type="com.mkyong.common.action.HtmlCheckBoxAction"
			name="htmlCheckBoxForm"
			validate="true"
			input="/pages/checkbox.jsp"
			>	
 
			<forward name="success" path="/pages/display.jsp"/>
		</action>
 
	</action-mappings>
 
	<message-resources
		parameter="com.mkyong.common.properties.Common" />
 
</struts-config>

7. web.xml

Final step, create a web.xml for the Strut framework integration.

<!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>

Access it

http://localhost:8080/StrutsExample/CheckBoxPage.do

Tick the check box and pressed on the submit button, it will forward to

http://localhost:8080/StrutsExample/CheckBox.do

If a check box is selected, the value is “on”, else it’s an empty value.

Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • Pingback: kangen()

  • Pingback: kangen water()

  • Pingback: best bottled water()

  • Pingback: water ionizers()

  • Pingback: stream movies()

  • Pingback: watch free movies online()

  • Pingback: free movie downloads()

  • Pingback: Blue Coaster33()

  • http://www.drhuqigong.co.uk/index.php/forum/2-welcome-mat/48111-q6ms---tory-burch-aih-zx#48111 goods

    I must point out my affection for your kind-heartedness giving support to individuals that must have help on this important subject. Your personal dedication to passing the message up and down became quite significant and have continually helped associates much like me to achieve their aims. This interesting publication denotes so much to me and extremely more to my peers. Thanks a ton; from all of us.

  • http://Website,com Name

    Note
    To post source code in comment, uses tag, for examples :

    Java source code here
    XML here

  • http://Website,com Name

    Website
    Website
    Website
    Website
    Website
    Website

  • Pavan

    You can change the value, instead as “on”, if the checkbox is selected.
    use the attribute ‘value’.

    When selected, it’s value is ‘Y’ and empty if not selected.

  • Jimmy

    “If a check box is selected, the value is “on”, else it’s an empty value.”

    There is no default value for checkboxValue.
    checkboxValue isn’t assigned as “on” whatever it’s checked or not

  • Pingback: html:checkbox tutorials, descriptions « Hopbit's Dev Blog()