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

Struts 2 與 Struts 1.x 深度比較:架構、Action、攔截器、IoC、測試、類型轉換全維度解析

     編輯:Java 相關知識

Struts 1.x 作為 MVC Model 2 經典框架風靡多年,但隨著 Web 發展暴露出代碼量大、類爆炸、單元測試難、Servlet API 耦合等痛點。Struts 2 (源自 WebWork 2.2 + Struts Ti 合併) 從架構層面徹底重設計,本文從 Action 模型、IoC、攔截器、全維度特性表 深度對比,助你評估遷移價值。

歷史脈絡

  • 2001/05 Struts 1.0 發布,確立 JSP+Servlet MVC 標準。
  • 2002/03 WebWork 1.0 發布,引入攔截器、IoC、OGNL 等創新。
  • 2005/12 WebWork 與 Struts Ti 合併 → Struts Action Framework 2.0 (即 Struts 2)。
  • Struts 1.x 與 WebWork 並行維護至今,但 Struts 2 是真正繼承者。

核心差異:Action 模型

維度Struts 1.xStruts 2
基類/介面必須繼承 org.apache.struts.action.Action (抽象類)POJO,無需繼承/實現任何介面 (慣例繼承 ActionSupport)
表單數據封裝獨立 ActionForm (FormBean)直接在 Action 屬性上 (Getter/Setter)
返回類型ActionForwardString (邏輯視圖名),或實現 Action 介面用常量
執行方法固定 execute(mapping, form, req, res)任意 public String xxx(),配置指定
參數獲取方法參數傳遞 HttpServletRequest/ResponseIoC 依賴注入 (Setter 注入、介面注入、構造器注入)
線程模型單例 Action,需手動同步屬性每請求一實例,天然線程安全

Struts 2 Action 範例


public class ActionForStruts2 extends ActionSupport {
    private String message;
    public String getMessage() { return message; }
    @Override
    public String execute() {
        message = "Hello from Struts2. Now: " + new Date();
        return SUCCESS; // 常量 "success"
    }
}

IoC (控制反轉) / DI (依賴注入) 深度解析

Struts 2 內建輕量級 IoC 容器 (源自 WebWork),並推薦整合 Spring 統一管理依賴。

介面注入範例:獲取 HttpServletRequest


public class IoCForStruts2 implements ServletRequestAware {
    private HttpServletRequest request;
    public void setServletRequest(HttpServletRequest request) { this.request = request; }
    public String execute() {
        // 直接使用 request 物件
        return Action.SUCCESS;
    }
}

關鍵點:Struts 2 每請求創建新 Action 實例,屬性天然線程安全,無需擔心單例併發問題。

攔截器機制

Struts 1.x 無內建攔截器 (需第三方 SAIF);Struts 2 以攔截器鏈為核心,國際化、類型轉換、驗證、文件上傳、事務管理等全部建構於攔截器之上。

攔截器鏈執行流程

攔截器鏈執行流程圖

  1. 請求到達 ServletDispatcher。
  2. 讀取配置,實例化攔截器串成 List。
  3. 依序調用 intercept(ActionInvocation invocation)。
  4. 每個攔截器可在 invocation.invoke() 前後執行邏輯,或直接返回結果短路。

內建豐富攔截器 (struts-default.xml)

  • params:參數注入 (OGNL 表達式賦值 Action 屬性)
  • conversionError:類型轉換錯誤處理
  • validation:驗證框架整合
  • workflow:驗證失敗返回 INPUT 結果
  • fileUpload:文件上傳支援
  • i18n:國際化語言切換
  • timer:性能監控

自定義攔截器極簡


public class MyInterceptor extends AbstractInterceptor {
    @Override public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Before action");
        String result = invocation.invoke();
        System.out.println("After action: " + result);
        return result;
    }
}

配置:<interceptor name="my" class="com.example.MyInterceptor"/> 並加入攔截器棧。

全維度特性對比表

特性Struts 1.xStruts 2
Action 類型繼承抽象類POJO + 可選介面
線程模型單例,需同步每請求一實例,無同步煩惱
Servlet 依賴強耦合 (execute 參數)無耦合 (Map 封裝),可選注入
易測試性需 Mock Servlet API (StrutsTestCase)直接 new Action()、set 屬性、呼叫方法,極易單測
輸入捕獲ActionForm (必須繼承基類)Action 屬性直接接收,支援豐富對象圖
表達式語言JSTL EL (集合/索引弱)OGNL (強大對象圖導航、投影、選擇)
值綁定頁面JSP pageContext setAttributeValueStack (多層級上下文,同名不同類型屬性共存)
類型轉換Commons-BeanUtils (全局配置)OGNL 類型轉換器 (可實例級配置)
驗證ActionForm validate() / Commons Validatorvalidate() + XWork 驗證框架 (字段/屬性/訪問器驗證,支援鏈式子屬性驗證)
Action 生命周期控制模塊級 RequestProcessor (所有 Action 共享)攔截器棧 (每 Action 可定制棧)

結論與遷移建議

  • Struts 2 ≠ Struts 1.x 升級版,它是 WebWork 升級版,僅提供兼容插件。
  • Struts 1.x 經驗不直接適用 Struts 2;WebWork 經驗完美遷移。
  • 新專案首選 Struts 2 (或 Spring MVC):POJO Action、攔截器 AOP、OGNL、ValueStack、易測試、零 Servlet 耦合。
  • 存量 Struts 1.x 專案:若穩定無擴展需求可維持;需重構/新增模塊時,建議模塊級遷移 (Struts 2 插件支援共存)。

本文基於 Struts 2.0.6 撰寫,核心架構設計至今仍具參考價值。

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