這里創(chuàng)建一個(gè)Web工程:struts2checkboxlist,來演示在多個(gè)復(fù)選框如何設(shè)置的默認(rèn)值,整個(gè)項(xiàng)目的結(jié)構(gòu)如下圖所示:
在Struts2,可以使用<s:checkboxlist>標(biāo)簽來使用相同的名稱來創(chuàng)建多個(gè)復(fù)選框。唯一的問題是如何把握變量中的多個(gè)檢查值? 例如,
public List<String> getColors() { colors = new ArrayList<String>(); colors.add("red"); colors.add("yellow"); colors.add("blue"); colors.add("green"); return colors; }
<s:checkboxlist label="What's your favor color" list="colors" name="yourColor" value="defaultColor" />
一個(gè)多復(fù)選框以“紅”,“黃”,“藍(lán)”和“綠色”為選項(xiàng)。如果有多個(gè)選項(xiàng)被選中,可以通過一個(gè)String對象存儲。
例如,如果“紅”“黃”選項(xiàng)被選中,選中的值將用逗號相結(jié)合連接,yourColor = “red,yellow”.
private String yourColor; public void setYourColor(String yourColor) { this.yourColor = yourColor; }
一個(gè)完整的Struts2實(shí)例,通過<s:checkboxlist>用相同的名稱創(chuàng)建多個(gè)復(fù)選框,存儲檢選中的值,并在另一頁面中顯示。
Action類來生成和保持的多個(gè)復(fù)選框值。
CheckBoxListAction.java
package com.yiibai.common.action; import java.util.ArrayList; import java.util.List; import com.opensymphony.xwork2.ActionSupport; public class CheckBoxListAction extends ActionSupport{ private List<String> colors; private String yourColor; public String getYourColor() { return yourColor; } public void setYourColor(String yourColor) { this.yourColor = yourColor; } public CheckBoxListAction(){ colors = new ArrayList<String>(); colors.add("red"); colors.add("yellow"); colors.add("blue"); colors.add("green"); } public String[] getDefaultColor(){ return new String [] {"red", "green"}; } public List<String> getColors() { return colors; } public void setColors(List<String> colors) { this.colors = colors; } public String execute() { return SUCCESS; } public String display() { return NONE; } }
通過“s:checkboxlist”標(biāo)簽渲染多個(gè)復(fù)選框。
checkBoxlist.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> </head> <body> <h1>Struts 2 multiple check boxes example</h1> <s:form action="resultAction" namespace="/"> <h2> <s:checkboxlist label="What's your favor color" list="colors" name="yourColor" value="defaultColor" /> </h2> <s:submit value="submit" name="submit" /> </s:form> </body> </html>
result.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <html> <body> <h1>Struts 2 multiple check boxes example</h1> <h2> Favor colors : <s:property value="yourColor"/> </h2> </body> </html>
<?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="checkBoxListAction" class="com.yiibai.common.action.CheckBoxListAction" method="display"> <result name="none">/pages/checkBoxlist.jsp</result> </action> <action name="resultAction" class="com.yiibai.common.action.CheckBoxListAction"> <result name="success">/pages/result.jsp</result> </action> </package> </struts>
http://localhost:8080/struts2checkboxlist/checkBoxListAction.action
http://localhost:8080/struts2checkboxlist/resultAction.action