# Struts2 <s:checkbox>核取方塊例子

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

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

## 預先選擇一個核取方塊

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

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

## 1. 動作 - Action

Action類有checkMe布林屬性來儲存核取方塊值。
CheckBoxAction.java

```package com.tw511.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. 結果頁面

checkBox.jsp

```<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 核取方塊範例</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 核取方塊範例</h1>

<h2>
CheckBox (CheckMe) value : <s:property value="checkMe"/>
</h2>

</body>
</html>```

## 3. struts.xml

```<?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.tw511.common.action.CheckBoxAction" method="display">
<result name="none">/pages/checkBox.jsp</result>
</action>

<action name="resultAction" class="com.tw511.common.action.CheckBoxAction">
<result name="success">/pages/result.jsp</result>
</action>
</package>

</struts>```

## 5. 範例

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

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