In Struts 2 , you can use the <s:checkbox> tag to create a HTML check box. ThefieldValue=”true” is the actual value that will be submitted by the check box.
<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/>
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;
}
}
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="/">
<h4>
<s:checkbox name="checkMe" fieldValue="true" label="Check Me for testing"/>
</h4>
<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>
<h4>
CheckBox (CheckMe) value : <s:property value="checkMe"/>
</h4>
</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>
http://localhost:8080/Struts2Example/checkBoxAction.action
http://localhost:8080/Struts2Example/resultAction.action
Reference
转自:http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/