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" %>
用於基本的條件流程控制。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> 時無法取值的問題。
遍歷 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>
<s:i18n name="ApplicationMessages"> 將指定資源包推入值棧,<s:text name="HelloWorld" /> 則輸出對應訊息。
包含 Servlet/JSP 輸出,並可傳遞參數。注意被包含頁面取得的是父頁面的請求參數。
<s:include value="/condition.jsp">
<s:param name="name">Max</s:param>
</s:include>
將複雜表達式結果存入指定範圍(application、session、request、page、action),提升效能與可讀性。
建構帶參數的 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>
輸出值棧頂端物件或指定 value 的屬性,支援 escape、default 屬性。
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>
對應上述表單的 UITagExample.java 需提供如 favouriteLanguages、vehicalTypeList、vehicalSpecificMap 等集合屬性,並實作 getVehicalSpecificList() 以支援雙層聯動邏輯。
| 維度 | Struts 1.x | Struts 2.0 |
|---|---|---|
| 分類方式 | HTML、Logic、Bean、Tiles | 統一命名空間,功能分非 UI / UI |
| 表達式語言 | JSTL EL | OGNL、JSTL、Groovy、Velocity |
| 標籤前綴 | html、logic、bean、tiles | 統一為 s(可自訂) |
掌握 Struts 2 核心標籤是高效開發 Web 應用的基礎。非 UI 標籤處理邏輯控制與資料綁定,UI 標籤則提供豐富的開箱即用元件。建議開發者結合官方 Showcase 範例,在實戰中熟練各標籤屬性與最佳實踐。