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

Struts 2.0 常用標籤完整指南:非 UI 與 UI 標籤詳解

     編輯:Java 相關知識

前言

Struts 2.0(原 WebWork 2)提供了功能強大且靈活的標籤庫,極大簡化了視圖層開發。與 Struts 1.x 將標籤分為 HTML、Logic、Bean、Tiles 等類別不同,Struts 2 所有標籤均位於 /struts-tags 命名空間下,功能上大致可分為「非 UI 標籤」與「UI 標籤」兩大類。本文將系統介紹最常用的核心標籤用法與範例。

標籤庫引入

在 JSP 頁面頂部加入以下指令即可使用預設前綴 s:

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

一、非 UI 標籤(控制流程與資料處理)

1. 條件控制:<s:if>、<s:elseif>、<s:else>

用於基本的條件流程控制。test 屬性接受 OGNL 表達式。

<s:set name="name" value="<%= '"' + request.getParameter('name') + '"' %>" />
<s:if test="#name == 'Max'">
  Max's file here
</s:if>
<s:elseif test="#name == 'Scott'">
  Scott's file here
</s:elseif>
<s:else>
  Other's file here
</s:else>

技巧:使用 <s:set> 將請求參數存入值棧,避免 #parameters 在 <s:include> 時無法取值的問題。

2. 迭代:<s:iterator>

遍歷 Collection、Iterator 或陣列。status 屬性可取得 IteratorStatus 物件,判斷奇偶、首尾等狀態。

<s:iterator value="#request.names" status="stuts">
  <s:if test="#stuts.odd == true">
    <li>White <s:property /></li>
  </s:if>
  <s:else>
    <li style="background-color:gray"><s:property /></li>
  </s:else>
</s:iterator>

3. 國際化:<s:i18n> 與 <s:text>

<s:i18n name="ApplicationMessages"> 將指定資源包推入值棧,<s:text name="HelloWorld" /> 則輸出對應訊息。

4. 頁面包含:<s:include> 與 <s:param>

包含 Servlet/JSP 輸出,並可傳遞參數。注意被包含頁面取得的是父頁面的請求參數。

<s:include value="/condition.jsp">
  <s:param name="name">Max</s:param>
</s:include>

5. 變數設定:<s:set>

將複雜表達式結果存入指定範圍(application、session、request、page、action),提升效能與可讀性。

6. URL 建構:<s:url> 與 <s:a>

建構帶參數的 URL,支援 includeParams 控制參數合併策略。

<s:url id="url" value="/condition.jsp">
  <s:param name="name">Max</s:param>
</s:url>
<s:a href="%{url}">if\elseif\else</s:a>

7. 屬性輸出:<s:property>

輸出值棧頂端物件或指定 value 的屬性,支援 escape、default 屬性。

二、UI 標籤(表單與介面元件)

UI 標籤分為表單標籤(對應 HTML 表單元素)與非表單標籤(佈局、訊息等)。Struts 2 新增了 datepicker、doubleselect、timepicker、optiontransferselect 等進階元件。

核心表單標籤範例

以下展示一個綜合範例(節錄自 Struts 2 Showcase),涵蓋文字輸入、日期選擇、下拉選單、複選框、雙層聯動選單、檔案上傳、選項轉移等:

<s:form action="exampleSubmit" method="post" enctype="multipart/form-data" tooltipConfig="#{'jsTooltipEnabled':'true'}">
  <s:textfield label="Name" name="name" tooltip="Enter your Name here" />
  <s:datepicker tooltip="Select Your Birthday" label="Birthday" name="birthday" />
  <s:textarea tooltip="Enter your Biography" label="Biograph" name="bio" cols="20" rows="3"/>
  <s:select tooltip="Choose Your Favourite Color" label="Favorite Color" list="{'Red', 'Blue', 'Green'}" name="favoriteColor" emptyOption="true" headerKey="None" headerValue="None"/>
  <s:select tooltip="Choose Your Favourite Language" label="Favourite Language" list="favouriteLanguages" name="favouriteLanguage" listKey="key" listValue="description" emptyOption="true" headerKey="None" headerValue="None"/>
  <s:checkboxlist tooltip="Choose your Friends" label="Friends" list="{'Patrick', 'Jason', 'Jay', 'Toby', 'Rene'}" name="friends"/>
  <s:checkbox tooltip="Confirmed that your are Over 18" label="Age 18+" name="legalAge"/>
  <s:doubleselect tooltip="Choose Your State" label="State" name="region" list="{'North', 'South'}" value="'South'" doubleValue="'Florida'" doubleList="top == 'North' ? {'Oregon', 'Washington'} : {'Texas', 'Florida'}" doubleName="state" headerKey="-1" headerValue="---------- Please Select ----------" emptyOption="true" />
  <s:doubleselect tooltip="Choose your Vehical" label="Favourite Vehical" name="favouriteVehicalType" list="vehicalTypeList" listKey="key" listValue="description" value="'MotorcycleKey'" doubleValue="'YamahaKey'" doubleList="vehicalSpecificList" doubleListKey="key" doubleListValue="description" doubleName="favouriteVehicalSpecific" headerKey="-1" headerValue="---------- Please Select ----------" emptyOption="true" />
  <s:file tooltip="Upload Your Picture" label="Picture" name="picture" />
  <s:optiontransferselect tooltip="Select Your Favourite Cartoon Characters" label="Favourite Cartoons Characters" name="leftSideCartoonCharacters" leftTitle="Left Title" rightTitle="Right Title" list="{'Popeye', 'He-Man', 'Spiderman'}" multiple="true" headerKey="headerKey" headerValue="--- Please Select ---" emptyOption="true" doubleList="{'Superman', 'Mickey Mouse', 'Donald Duck'}" doubleName="rightSideCartoonCharacters" doubleHeaderKey="doubleHeaderKey" doubleHeaderValue="--- Please Select ---" doubleEmptyOption="true" doubleMultiple="true" />
  <s:textarea label="Your Thougths" name="thoughts" tooltip="Enter your thoughts here" />
  <s:submit onclick="alert('aaaa');" />
  <s:reset onclick="alert('bbbb');" />
</s:form>

後端 Action 類別關鍵屬性

對應上述表單的 UITagExample.java 需提供如 favouriteLanguages、vehicalTypeList、vehicalSpecificMap 等集合屬性,並實作 getVehicalSpecificList() 以支援雙層聯動邏輯。

三、Struts 2 標籤庫對比總結

維度Struts 1.xStruts 2.0
分類方式HTML、Logic、Bean、Tiles統一命名空間,功能分非 UI / UI
表達式語言JSTL ELOGNL、JSTL、Groovy、Velocity
標籤前綴html、logic、bean、tiles統一為 s(可自訂)

參考資源

結語

掌握 Struts 2 核心標籤是高效開發 Web 應用的基礎。非 UI 標籤處理邏輯控制與資料綁定,UI 標籤則提供豐富的開箱即用元件。建議開發者結合官方 Showcase 範例,在實戰中熟練各標籤屬性與最佳實踐。

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