入力フィールドにcheckboxを使った場合の処理です。
checkboxが一つの場合
Action
booleanのsetterを書きます(初期値はfalse)
package ww2.examples.event; import com.opensymphony.xwork.ActionSupport; public class SingleCheckboxAction extends ActionSupport { private boolean check; public String execute() throws Exception { System.out.println("check:"+check); return SUCCESS; } public boolean isCheck() { return check; } public void setCheck(boolean check) { this.check = check; } }
vmファイル(singlecheckbox.vm)
チェックボックスのvalueはtrueにしておきます。
<form action="singlecheckbox.action" method="POST"> <input type="checkbox" name="check" value="true" /> <input type="submit" value="実行" /> </form>
xwork.xml
<action name="singlecheckbox" class="ww2.examples.event.SingleCheckboxAction"> <interceptor-ref name="defaultStack"/> <result name="success" type="dispatcher"> <param name="location">/WEB-INF/vm/singlecheckbox.vm</param> </result> </action>
結果
チェックボックスがチェックされた場合は、Actionのcheckがtrueになり、チェックされない場合はfalseになります。
checkboxが複数の場合
Actionにsetterを書く
String配列(java.util.List,java.util.Setでも可)のsetterを書きます。
package ww2.examples.event; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import com.opensymphony.xwork.ActionSupport; public class MultiCheckboxAction extends ActionSupport { private String[] check1 = new String[0]; private List check2 = new ArrayList(); private Set check3 = new HashSet(); public String execute() throws Exception { for (int i = 0; i < check1.length; i++) { System.out.println("check1:" + check1[i]); } for (Iterator iter = check2.iterator(); iter.hasNext();) { System.out.println("check2:" + iter.next()); } for (Iterator iter = check3.iterator(); iter.hasNext();) { System.out.println("check3:" + iter.next()); } return SUCCESS; } public String[] getCheck1() { return check1; } public void setCheck1(String[] check1) { this.check1 = check1; } public List getCheck2() { return check2; } public void setCheck2(List check2) { this.check2 = check2; } public Set getCheck3() { return check3; } public void setCheck3(Set check3) { this.check3 = check3; } }
vmファイル
<form action="multicheckbox.action" method="POST"> check1 <input type="checkbox" name="check1" value="a" /> <input type="checkbox" name="check1" value="b" /> <input type="checkbox" name="check1" value="c" /><br /> check2 <input type="checkbox" name="check2" value="a" /> <input type="checkbox" name="check2" value="b" /> <input type="checkbox" name="check2" value="c" /><br /> check3 <input type="checkbox" name="check3" value="a" /> <input type="checkbox" name="check3" value="b" /> <input type="checkbox" name="check3" value="c" /><br /> <input type="submit" value="実行" /> </form>
xwork.xml
<action name="multicheckbox" class="ww2.examples.event.MultiCheckboxAction"> <interceptor-ref name="defaultStack"/> <result name="success" type="dispatcher"> <param name="location">/WEB-INF/vm/multicheckbox.vm</param> </result> </action>
結果
チェックボックスがチェックされた場合は、チェックされた値が配列に入ります。一つもチェックされなかった場合はActionの初期値のままです。