Struts 2 <s:checkbox> checkbox example

In Struts 2 , you can use the <s:checkbox> tag to create a HTML check box. The fieldValue=”true” is the actual value that will be submitted by the check box.

<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/>
In common, you do not need to declared the fieldValue=”true”, because true is the default value.

It will generate the following HTML.

<input type="checkbox" name="checkMe" value="true" id="xx_checkMe"/>
<input type="hidden" id="__checkbox_xx_checkMe" name="__checkbox_checkMe" value="true"/>
<label for="resultAction_checkMe" class="checkboxLabel">Check Me for testing</label>

Preselect a checkbox

If you want to preselect a check box, just add a value attribute and set it to true.

<s:checkbox name="checkMe" fieldValue="true" value="true" label="Check Me for testing"/>

It will generate the following HTML.

<input type="checkbox" name="checkMe" value="true" checked="checked" id="xx_checkMe"/>
<input type="hidden" id="__checkbox_xx_checkMe" name="__checkbox_checkMe" value="true" />
<label for="resultAction_checkMe" class="checkboxLabel">Check Me for testing</label>

Struts 2 <s:checkbox> example

A full example to create a check box via Struts 2 <s:checkbox>, and assign submitted check box value to the Action class and display it.

1. Action

Action class with a checkMe boolean property to hold the check box value.
CheckBoxAction.java

package com.mkyong.common.action;
 
import com.opensymphony.xwork2.ActionSupport;
 
public class CheckBoxAction extends ActionSupport{
 
	private boolean checkMe;
 
	public boolean isCheckMe() {
		return checkMe;
	}
 
	public void setCheckMe(boolean checkMe) {
		this.checkMe = checkMe;
	}
 
	public String execute() {
 
		return SUCCESS;
 
	}
 
	public String display() {
 
		return NONE;
 
	}
 
}

2. Result page

Result page to use Struts 2 “s:checkbox” tag to create a check box.

checkBox.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>
 
<body>
<h1>Struts 2 check box example</h1>
 
<s:form action="resultAction" namespace="/">
 
<h2>
	<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/>
</h2> 
 
<s:submit value="submit" name="submit" />
 
</s:form>
 
</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 
<body>
<h1>Struts 2 check box example</h1>
 
<h2>
  CheckBox (CheckMe) value : <s:property value="checkMe"/>
</h2> 
 
</body>
</html>

3. struts.xml

Link all together ~

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>
 
 <constant name="struts.devMode" value="true" />
 
<package name="default" namespace="/" extends="struts-default">
 
   <action name="checkBoxAction" 
         class="com.mkyong.common.action.CheckBoxAction" method="display">
	<result name="none">pages/checkBox.jsp</result>
   </action>
 
   <action name="resultAction" class="com.mkyong.common.action.CheckBoxAction">
	<result name="success">pages/result.jsp</result>
   </action>
  </package>
 
</struts>

5. Demo

http://localhost:8080/Struts2Example/checkBoxAction.action

Struts2 check box

http://localhost:8080/Struts2Example/resultAction.action

Struts2 check box

Reference

  1. Struts 2 checkbox documentation
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: pay per day loans plan()

  • Pingback: stop parking()

  • Pingback: penge lan nu()

  • Pingback: mobile porn()

  • Pingback: stop parking()

  • Pingback: tv packages()

  • Pingback: watch movies online()

  • Pingback: Blue Coaster33()

  • http://www.buscocolegio.com peter saurus

    nice tutorial but could you complete it with how to get the s:checkbox value?? checboxes usually work showing or hidding another parts of the screen, depending on whether they are selected/unselected

  • http://nitinsurana.com Nitin

    dude, I tried creating a checkbox example. Everything seems to work fine BUT WITH A OGNL WARNING

    [2012-12-25 02:04:43.565] [ WARN] OgnlValueStack.warn:49 - Error setting expression '__checkbox_account.privacyopenorders' with value '[Ljava.lang.String;@2222da8f'
    ognl.OgnlException: target is null for setProperty(null, &quot;privacyopenorders&quot;, [Ljava.lang.String;@2222da8f)
  • sri

    Hi,

    You display html code of check box. How do you get it through javascript please help me.

    Thanks,
    sri.

  • Jayson

    Hi Sir,

    Is there an example of checkbox inside the iterator tag?

  • Suresh

    Fine Example

  • Pingback: Struts 2 Tutorials()