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.

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.


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


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

3. struts.xml

Link all together ~

<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 <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 name="resultAction" class="com.mkyong.common.action.CheckBoxAction">
	<result name="success">pages/result.jsp</result>

5. Demo


Struts2 check box


Struts2 check box


  1. Struts 2 checkbox documentation
Tags :

About the Author

Founder of and, 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.


  • Pingback: a grade electricians()

  • Pingback: click()

  • Pingback: house blue()

  • Pingback: p trap plumbers putty()

  • Pingback: electrician tool belt()

  • Pingback: water ionizer()

  • 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()

  • 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

  • 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


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


  • Jayson

    Hi Sir,

    Is there an example of checkbox inside the iterator tag?

  • Suresh

    Fine Example

  • Pingback: Struts 2 Tutorials()