首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Struts 動態複選框最佳實踐:html:multibox + logic:iterate 實現動態渲染與預選

     編輯:Java 相關知識

在 Struts 1.x 中構建動態複選框組(如權限分配、標籤篩選、多選題)是常見需求。本文展示利用 <html:multibox> 搭配 <logic:iterate> 實現:從後端動態獲取選項列表、根據表單屬性自動預選、提交時自動收集勾選值到 String 陣列的完整方案,並延伸示範 LabelValueBean 支援顯示值與提交值分離。

核心原理

<html:multibox property="selectedItems"> 渲染為 ``。當表單提交時,Struts 自動將所有勾選的 checkbox 值收集到 ActionForm 的 String[] selectedItems 屬性。迴圈渲染時,若迴圈變量值存在於 selectedItems 陣列中,該 checkbox 自動加上 `checked="checked"`。

完整實現三步驟

1. ActionForm 定義選項與預選屬性

public class CheckboxTestForm extends ActionForm {
    // 所有可選項(如從資料庫查詢動態設置)
    private String[] mountains = {
        "Everest", "K2", "Kangchenjunga", "Lhotse", "Makalu",
        "Kangchenjunga South", "Lhotse Middle", "Kangchenjunga West",
        "Lhotse Shar", "Cho Oyu"
    };
    // 預選/提交勾選項
    private String[] selectedMountains = {"Everest", "K2", "Lhotse"}; // 初始預選

    // Getter/Setter 省略...
}

2. JSP 動態渲染複選框組

<%@ taglib uri="https://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="https://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="https://struts.apache.org/tags-logic" prefix="logic" %>

<html:form action="/FormAction" name="testForm" type="com.example.CheckboxTestForm">
    <h4><bean:message key="testForm.instruction"/></h4>

    <logic:iterate name="testForm" property="mountains" id="mountain">
        <!-- 複選框:property 指向提交收集屬性,body 為提交值 -->
        <html:multibox property="selectedMountains">
            <bean:write name="mountain"/>
        </html:multibox>
        <!-- 標籤顯示 -->
        <bean:write name="mountain"/><br/>
    </logic:iterate>

    <br/>
    <html:submit/> <html:reset/>
</html:form>

關鍵點:

  • name="testForm" 對應 ActionForm Bean 名稱。
  • property="mountains" 迴圈來源陣列/集合。
  • id="mountain" 當前迴圈項暴露為 pageContext 屬性。
  • <html:multibox property="selectedMountains"> body 內容作為 checkbox 的 `value` 屬性。

3. Action 處理提交數據

public class CheckboxTestAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        CheckboxTestForm tf = (CheckboxTestForm) form;
        String[] selected = tf.getSelectedMountains(); // 自動收集勾選值
        // 業務處理:保存權限、記錄偏好等
        HttpSession session = request.getSession();
        session.setAttribute("selectedMountains", selected);
        return mapping.findForward("success");
    }
}

進階:LabelValueBean 實現顯示值與提交值分離

實際場景常需「顯示中文名,提交英文代碼/ID」。使用 Struts 內建 org.apache.struts.util.LabelValueBean:

Form 屬性改為 List

private List mountainsLb = new ArrayList<>();
// 初始化:
mountainsLb.add(new LabelValueBean("珠穆朗瑪峰", "Everest"));
mountainsLb.add(new LabelValueBean("喬戈里峰", "K2"));
// ...

JSP 迴圈解包 LabelValueBean

<logic:iterate name="testForm" property="mountainsLb" id="mountainLb">
    <bean:define id="mountainBean" name="mountainLb" type="org.apache.struts.util.LabelValueBean"/>
    <html:multibox property="selectedMountains">
        <bean:write name="mountainBean" property="value"/> <!-- 提交值 -->
    </html:multibox>
    <bean:write name="mountainBean" property="label"/><br/> <!-- 顯示標籤 -->
</logic:iterate>

<bean:define> 將迴圈項轉型為 LabelValueBean 並暴露為 mountainBean,後續 property="value/label" 精確取值。

結果頁面展示勾選項

<logic:iterate id="mountain" property="mountains" name="testForm">
    <bean:write name="mountain"/><br/>
</logic:iterate>
<hr size="5" color="black"/>
<logic:iterate id="sel" property="selectedMountains" name="testForm">
    <bean:write name="sel"/><br/>
</logic:iterate>

避坑與最佳實踐

  • 集合類型:mountains 可為 Array、List、Collection,logic:iterate 均支援。
  • 初始預選:在 Action 或 Form 初始化時給 selectedMountains 賦值,頁面載入即自動勾選。
  • 全選/反選:需前端 JS 實現,Struts 標籤不直接支援。
  • 大量選項分頁:建議結合 DisplayTag 或自定義分頁標籤,避免單頁渲染過多 DOM 節點。
  • Struts 2 遷移:Struts 2 使用 <s:checkboxlist> 或 <s:iterator> + <s:checkbox>,原理相似但標籤體系不同。

來源致謝

本文整理自 IBM developerWorks 文章《Struts的動態復選框》,作者 Danilo Gurovich(Struts Recipes 合著者)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved