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

Struts 1.x 核心架構深度剖析:Controller、Action、ActionForm 與標籤庫

     編輯:Java 相關知識

Struts 核心職責

Struts 作為經典 MVC 框架,Controller 層主要執行四大任務:

  1. 攔截用戶 HTTP 請求
  2. 將請求映射到預定義的業務操作
  3. 獲取業務操作結果,提供給視圖層
  4. 決定下一個導向視圖頁面

Controller 組件協作流程

HTTP Request
    ↓
ActionServlet (Front Controller)
    ↓
RequestProcessor (處理流程模板方法)
    ↓
查找 ActionMapping (struts-config.xml)
    ↓
ActionForm 驗證/填充
    ↓
Action.execute() 業務處理
    ↓
返回 ActionForward
    ↓
RequestProcessor 轉向/重導向對應 JSP
    ↓
HTTP Response

核心類別詳細解析

ActionServlet:總控制器

  • 繼承 HttpServlet,所有請求入口。
  • process() 方法委派給 RequestProcessor。
  • 通常無需繼承覆寫,除非需要全域性前置/後置處理(如自定義多語系初始化)。

RequestProcessor:流程處理器

  • 模板方法模式,process() 定義標準流程骨架。
  • 關鍵可覆寫方法:
    • processLocale():動態切換用戶語系(預設僅啟動時載入一次)。
    • processActionPerform():調用 Action.execute()。
    • processForward():處理 ActionForward 導向邏輯。
  • 配置檔指定自定義 Processor:<controller processorClass="com.example.MyRequestProcessor"/>

Action:業務邏輯入口(核心原則:薄控制器)

public class LoginAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        // 1. 取得表單資料
        LoginForm loginForm = (LoginForm) form;
        // 2. 委派給 Service 層 (Model) - 絕不在 Action 寫業務邏輯!
        UserService service = ServiceLocator.getUserService();
        User user = service.authenticate(loginForm.getUsername(), loginForm.getPassword());
        // 3. 放入請求/會話屬性供視圖使用
        if (user != null) {
            request.getSession().setAttribute("user", user);
            return mapping.findForward("success");
        } else {
            // 4. 錯誤處理
            ActionErrors errors = new ActionErrors();
            errors.add("login", new ActionMessage("error.login.failed"));
            saveErrors(request, errors);
            return mapping.findForward("failure");
        }
    }
}

鐵律:Action 僅負責流程控制(參數轉換、Service 調用、結果導向),嚴禁包含業務邏輯。struts-example 範例專案將業務邏輯寫在 Action 中是反模式。

ActionForm:表單資料傳輸與驗證

  • 作為緩衝區暫存用戶輸入,隔離 HTTP 請求參數與業務層。
  • 提供 validate() 方法進行伺服器端驗證,失敗時自動返回輸入頁面顯示錯誤(配合 <html:errors/>)。
  • 兩種 Scope:
    • request:請求結束即銷毀(預設,安全)。
    • session:跨請求保留(適用於多步驟精靈表單)。

ActionForward:解耦物理視圖路徑

<action-mappings>
  <action path="/login" type="com.example.LoginAction" name="loginForm" scope="request" validate="true" input="/login.jsp">
    <forward name="success" path="/welcome.do" redirect="true"/>  <!-- 重導向防止重複提交 -->
    <forward name="failure" path="/login.jsp"/>
  </action>
</action-mappings>

ActionMapping.findForward("success") 會依序查找:Local Forward → Global Forward → 拋出異常。

四大標籤庫速查

標籤庫 URI前綴主要用途
https://struts.apache.org/tags-beanbeanBean 存取、屬性輸出、訊息國際化 (bean:message, bean:write)
https://struts.apache.org/tags-htmlhtml表單生成 (html:form, html:text, html:select)、連結、錯誤顯示 (html:errors)
https://struts.apache.org/tags-logiclogic流程控制 (logic:iterate, logic:present, logic:equal)、條件輸出
https://struts.apache.org/tags-templatetemplate模板機制 (已被 Tiles 取代)

遷移建議:Struts 1 → Spring MVC / Struts 2

  • Struts 1 已於 2013 年宣布 EOL,存在已知安全漏洞 (CVE-2017-5638 等)。
  • Spring MVC:註解驅動、REST 支援、完美整合 Spring 生態、易測試、現代標準選擇。
  • Struts 2:攔截器架構、OGNL 表達式、約定優於配置、動作類為 POJO、但 OGNL 歷史安全問題較多。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved