ダウンロードする – リンク://wp-content/uploads/2010/06/Struts2-CheckBox-Example.zip[Struts2-CheckBox-Example.zip]

Struts 2では、

<s:checkbox>

タグを使用してHTMLチェックボックスを作成できます。

fieldValue = “true”

は、チェックボックスによって送信される実際の値です。

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

一般的には、

fieldValue = “true”

を宣言する必要はありません。これは、trueがデフォルト値であるためです。

次の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

チェックボックスを事前に選択する場合は、値属性を追加してtrueに設定します。

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

次の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:チェックボックス>


Struts 2

<s:checkbox>

を使用してチェックボックスを作成し、送信されたチェックボックスの値をActionクラスに割り当てて表示する完全な例。

1.アクション

チェッククラスの値を保持する

checkMe

booleanプロパティを持つActionクラス。

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.結果ページ

Struts 2 ”

s:checkbox

“タグを使用してチェックボックスを作成する結果ページ。

<%@ 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

すべて一緒にリンクする〜

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

リファレンス

ドキュメンテーション]