Struts 2 <s:checkbox> checkbox example

 

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"/>
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="/">
 
<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>

 
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

转自:http://www.mkyong.com/struts2/struts-2-scheckbox-checkbox-example/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值