Apache Struts 2 是企業級 Java Web 框架的主流選擇之一,源自 WebWork 2 與 Struts 社群合併,提供更簡潔的 POJO Action、強大的攔截器機制、OGNL 表達式語言與零配置傾向。本章將手把手帶領讀者完成 Struts 2.1.0 開發環境搭建,並撰寫第一個 HelloWorld 應用。
| 特性 | Struts 1.x | Struts 2 |
|---|---|---|
| Action 類別 | 繼承抽象基類 Action | POJO,可選實作 Action 介面或繼承 ActionSupport |
| 執行緒模型 | 單例,需自行處理執行緒安全 | 每請求一實例,無執行緒安全顧慮 |
| Servlet 依賴 | Action 方法簽名綁定 HttpServletRequest/Response | 不依賴容器,易於單元測試 |
| 輸入處理 | 需繼承 ActionForm | 直接使用 Action 屬性,支援豐富領域物件 |
| 表達式語言 | JSTL EL | OGNL(更強大的物件圖導航) |
| 值綁定 | JSP 標準機制 | ValueStack 技術,解耦視圖與模型 |
| 型別轉換 | Commons-Beanutils,每類一轉換器 | OGNL 內建轉換器,可配置 |
| 驗證機制 | validate() 或 Commons Validator | XWork 驗證框架,支援鏈式驗證子屬性 |
| Action 控制 | 模組級 RequestProcessor | 攔截器堆疊,每 Action 可自訂生命週期 |
官方下載頁:https://people.apache.org/builds/struts/。最新版 2.1.0(2007-10-29)提供多種套件:
| 檔案 | 用途 |
|---|---|
struts-2.1.0-docs.zip | 文件與 API |
struts-2.1.0-lib.zip | 建構專案所需 JAR |
struts-2.1.0-src.zip | 原始碼 |
struts2-blank-2.1.0.war | 空白專案範本 |
struts-2.1.0-all.zip | 全包(含以上所有) |
核心 5 支 JAR(從 struts2-blank.war 的 WEB-INF/lib 可見):
commons-logging-1.0.4.jar:日誌抽象層freemarker-2.3.8.jar:視圖模板引擎(主題渲染)ognl-2.6.11.jar:OGNL 表達式語言struts2-core-2.0.10.jar:Struts 2 核心xwork-2.0.4.jar:WebWork 核心(攔截器、ValueStack 等)

Struts2 


Struts2Demo 
Struts2 


<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Struts 2 以 FilterDispatcher 取代 Struts 1 的 ActionServlet,預設攔截 *.action 請求。
包 com.lizanhong.action,繼承 ActionSupport:
package com.lizanhong.action;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
@Override
public String execute() throws Exception {
System.out.println("Action執行了。");
return SUCCESS;
}
}
ActionSupport 提供 getText()(國際化)、addFieldError()(驗證錯誤)等便利方法。
置於類別路徑根目錄(Eclipse src 下):
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "https://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package name="a" extends="struts-default">
<action name="helloworld" class="com.lizanhong.action.HelloWorldAction">
<result>/result.jsp</result>
</action>
</package>
</struts>
關鍵元素說明:
<include>:模組化配置,引入預設攔截器堆疊等定義。<package>:Action/攔截器分組,extends="struts-default" 繼承預設配置。<action>:name 對應 URL(helloworld.action),class 指定 Action 類別。<result>:Action 返回 SUCCESS 時導向 /result.jsp。預設 name="success"。<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><base href="<%=basePath%>"><title>Result</title></head>
<body>世界,你好. <br></body></html>
https://localhost:8081/Struts2Demo/helloworld.action。Action執行了。,頁面顯示 世界,你好。DTD 定義了完整配置元素:<struts> 根元素包含 package、include、bean、constant;package 內含 action、interceptor、result-type 等;action 可嵌套 param、result、interceptor-ref、exception-mapping。完整 DTD 內容請參考原始檔案。
Struts 2 以 POJO Action、攔截器鏈、OGNL 與 ValueStack 重新定義了 MVC 框架標準。本章示範的「最小可行配置」——web.xml Filter、ActionSupport 子類、struts.xml 單一 package/action/result、JSP 結果頁——即為所有 Struts 2 應用的基石。掌握此流程,後續擴展攔截器、驗證、國際化、Tiles 整合皆建立在相同基礎之上。