Struts 2 採用過濾器攔截請求,核心流程如下:
ActionContextCleanUp,利於 SiteMesh 等插件整合)。FilterDispatcher 接管請求,委託 ActionMapper 判斷是否需調用 Action。FilterDispatcher 將處理權交給 ActionProxy。ActionProxy 透過 Configuration Manager 讀取配置,定位 Action 類。ActionInvocation 實例,按命名模式調用 Action,過程中穿插攔截器(Interceptor)執行。ActionInvocation 根據 struts.xml 尋找對應結果(通常為 JSP/FreeMarker),過程中可使用 Struts 標籤。所有物件(Action、Result、Interceptor 等)均由 ObjectFactory 創建。

<struts>
<!-- 開發模式,輸出詳細錯誤 -->
<constant name="struts.devMode" value="true" />
<!-- 國際化編碼 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 允許 OGNL 訪問靜態方法 -->
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<!-- 處理的請求後綴,預設 action -->
<constant name="struts.action.extension" value="action"/>
<!-- 瀏覽器靜態資源緩存,開發關閉 -->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 配置文件熱重載 -->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 預設 UI 主題 -->
<constant name="struts.ui.theme" value="simple"/>
<!-- Spring 管理 Action -->
<constant name="struts.objectFactory" value="spring"/>
<!-- 關閉動態方法調用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- 上傳檔案大小限制 -->
<constant name="struts.multipart.maxSize" value="10701096"/>
<!-- 模組化引入子配置 -->
<include file="cn/qjc/action/login/login.xml"/>
<include file="cn/qjc/action/demo/demo01.xml"/>
<include file="cn/qjc/interceptor/interceptor.xml"/>
</struts>
default.properties(核心 JAR 內,唯讀)struts-default.xml(核心 JAR 內,唯讀)struts-plugin.xml(插件 JAR 內,唯讀)struts.xml(專案 src 根目錄,**推薦**)struts.properties(專案根目錄,不推薦)web.xml filter 參數(不推薦)
struts-default,否則核心功能失效/ 開頭,訪問路徑 = namespace + action 名稱;空字串為預設命名空間ActionSupportexecute()<package name="default" extends="struts-default" abstract="true">
<global-results>
<result name="success">/WEB-INF/login/success.jsp</result>
</global-results>
</package>
<package name="login" namespace="/user" extends="default">
<action name="login" class="cn.qjc.action.login.Login" method="login">
<result name="success" type="dispatcher">/WEB-INF/login/success.jsp</result>
<result name="error">/WEB-INF/login/error.jsp</result>
</action>
</package>
Action 介面:需實現 execute(),提供 SUCCESS 等常數。ActionSupport(推薦):內建驗證、國際化、預設 execute() 返回 success 等功能。ActionSupport 實現了 Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable 等介面。若 <action> 未指定 class,框架會使用 struts-default.xml 中定義的 <default-class-ref class="com.opensymphony.xwork2.ActionSupport" />。
Struts 預設提供 10 種結果類型(struts-default.xml 定義),預設為 dispatcher(請求轉發)。
| 類型 | 說明 | 典型用法 |
|---|---|---|
| dispatcher | 請求轉發(預設) | <result>/page.jsp</result> 或 <param name="location">/page.jsp</param> |
| redirect | 重定向,丟失請求參數/屬性 | 不可重定向到 /WEB-INF/ 下資源 |
| redirectAction | 重定向到另一 Action | 支援 actionName、namespace 參數 |
| chain | Action 鏈 | 轉發到另一 Action 並保留狀態 |
| stream | 檔案下載 | 配合 inputName 等參數 |
| freemarker/velocity | 模板引擎 | 頁面靜態化 |
| plainText | 純文本輸出 | 輸出原始碼 |
com.opensymphony.xwork2.Result(建議繼承 StrutsResultSupport),重寫 doExecute。<result-types> 中聲明:<result-type name="captcha" class="cn.qjc.action.demo.Demo02"/><result name="success" type="captcha"><param name="width">200</param></result>範例:驗證碼圖片直接輸出到響應流。

Struts 2 的 Action 預設為**多例**(Prototype):每次請求創建新實例,天然線程安全。驗證方式:在構造函數輸出日誌,連續請求兩次觀察是否打印兩次。
public class MoreInstanceAction extends ActionSupport {
public MoreInstanceAction() { System.out.println("create new action"); }
public String execute() { System.out.println("more instance action"); return SUCCESS; }
}