Struts2教學
Struts2快速入門
Struts2 MVC架構
Struts2 是什麼?
Struts2 環境配置
Struts2 體系結構
Struts2 Hello World 實例
Struts2 配置文件
Struts2 Action/動作
Struts2 攔截器
Struts2 結果和結果類型
Struts2 值棧/OGNL
Struts2 文件上傳
Struts2 MySQL數據庫訪問
Struts2 發送郵件Email
Struts2 驗證框架
Struts2 基於XML校驗
Struts2 本地化/國際化(i18n)
Struts2 類型轉換
Struts2 主題和模板
Struts2 異常處理
Struts2 註釋
Struts2 註釋類型
Struts2 控件標籤
Struts2 iterator標籤
Struts2 if/else標籤
Struts2 merge標籤
Struts2 append標籤
Struts2 generator標籤
Struts2 數據標籤
Struts2 action標籤
Struts2 include標籤
Struts2 bean 標籤
Struts2 date 標籤
Struts2 param 標籤
Struts2 property 標籤
Struts2 set 標籤
Struts2 text 標籤
Struts2 url標籤
Struts2 表單標籤
Struts2 Ajax 標籤
Struts2和Spring集成
Struts2和Tiles集成
Struts2和Hibernate集成
Struts 2 hello world (XML版本)
Struts2註解示例
Struts2 @ResultPath註釋示例
Struts2 include(包含)多個配置文件
Struts2命名空間配置和解釋
Struts2開發者模式
如何刪除Struts2動作的後綴擴展名
使用Struts2動作
Struts2的ActionError&ActionMessage示例
Struts2模型驅動實例
Struts2映射攔截動作
Struts2重寫攔截器參數
Struts2攔截器棧的例子
Struts2 execAndWait攔截器例子
Struts2 <s:textfield>文本示例
Struts2 <s:password>密碼示例
Struts2 <s:hidden>隱藏值例子
Struts2 <s:textarea>文本域實例
Struts2 <s:radio>單選按鈕例子
Struts2單選預選按鈕值
Struts2 <s:checkbox>複選框例子
Struts2設置多個複選框缺省值
Struts2 <s:checkboxlist>多個複選框例子
Struts2 <s:select>下拉框例子
Struts2自動選擇下拉框的值
Struts2 <s:combobox>組合框的例子
Struts2 <s:head>示例
Struts2文件上傳例子
Struts2上傳多個文件例子
Struts2 <s:doubleselect>示例
Struts2 <s:updownselect>示例
Struts2 <s:optiontransferselect>示例
Struts2 <sx:datetimepicker>示例
Struts2 <sx:autocompleter>自動提示例子
Struts2 autocompleter+JSON例子
Struts2 <s:iterator>迭代器標籤示例
Struts2 if,elseif,else標籤示例
struts2 <s:append>標籤示例
Struts2 <s:generator>標籤示例
Struts2 <s:merge>標籤示例
Struts2 <s:sort>標籤示例
Struts2 <s:subset>標籤示例
Struts2 <s:a>標籤示例
Struts2 <s:action>標籤示例
Struts2 <s:bean>標籤示例
Struts2 <s:date>標籤示例
Struts2 <s:debug>標籤示例
Struts2 <s:include>標籤示例
Struts2 <s:i18n>標籤示例
Struts2 <s:param>標籤示例
Struts2 <s:property>標籤實例
Struts2 <s:push>標籤示例
Struts2 <s:set>標籤示例
Struts2 <s:texttag>標籤示例
Struts2 <s:url>標籤示例
Struts2資源包使用示例
Struts2本地化和國際化
Struts2 key鍵屬性示例
Struts2中文本地化問題
如何配置Struts2全局資源包
Struts2+Spring集成實例
Struts2+Quartz2調度集成實例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2+Hibernate集成實例
Struts2+Spring+Hibernate集成實例
Struts2+Log4j集成
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什麼不同?
Struts2獲取HttpServletRequest實例
Struts2獲取HttpServletResponse實例
如何獲取ServletContext對象
Struts2配置Action類的靜態參數
Struts2下載文件實例
Struts2和JSON實例

Struts2 <s:checkboxlist>多個複選框例子

這裏創建一個Web工程:struts2checkboxlist,來演示在多個複選框如何設置的默認值,整個項目的結構如下圖所示:

Struts2

在Struts2,可以使用<s:checkboxlist>標籤來使用相同的名稱來創建多個複選框。唯一的問題是如何把握變量中的多個檢查值? 例如,

public List getColors() {
colors = new ArrayList();
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" />

一個多複選框以「紅」,「黃」,「藍」和「綠色」爲選項。如果有多個選項被選中,可以通過一個String對象存儲。

例如,如果「紅」「黃」選項被選中,選中的值將用逗號相結合連接,yourColor = 「red,yellow」.

private String yourColor;

public void setYourColor(String yourColor) {
this.yourColor = yourColor;
}

閱讀這篇文章,有關如何設置多個複選框的默認值。

Struts2 <s:checkboxlist> 示例

一個完整的Struts2實例,通過<s:checkboxlist>用相同的名稱創建多個複選框,存儲檢選中的值,並在另一頁面中顯示。

1. 動作 - Action

Action類來生成和保持的多個複選框值。
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;
}

}

2. 結果頁面

通過「s:checkboxlist」標籤渲染多個複選框。
checkBoxlist.jsp

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

Struts 2 multiple check boxes example

<s:form action="resultAction" namespace="/">

<s:submit value="submit" name="submit" />

result.jsp

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

Struts 2 multiple check boxes example

Favor colors :

3. struts.xml


/pages/checkBoxlist.jsp

/pages/result.jsp

5. 實例

http://localhost:8080/struts2checkboxlist/checkBoxListAction.action

Struts2

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

* Struts2
*

參考

  1. Struts 2 checkboxlist 文檔

下載代碼 –  http://pan.baidu.com/s/1dDCxAiH