Struts 是 Apache Jakarta 專案旗下經典 MVC 框架,基於 Servlet、JSP、JavaBeans、XML 等標準技術,實現表現邏輯與業務邏輯分離。本文系統梳理 Struts 1.x 的架構圖譜、核心組件職責、struts-config.xml 配置詳解與請求處理流程。

| 層級 | Struts 實現 | 說明 |
|---|---|---|
| Model | ActionForm Bean、Action、Business Delegate、DAO、EJB/Hibernate/iBATIS | 內部狀態與業務操作封裝,ActionForm 保存表單資料,Action 協調業務邏輯 |
| View | JSP + Struts TagLib (Bean/HTML/Logic/Nested/Template) | 國際化標籤庫簡化介面開發,支援 JSTL、Velocity、XSLT 等模板引擎 |
| Controller | ActionServlet、ActionMapping、ActionForward、struts-config.xml | ActionServlet 為核心控制器,根據映射分派請求至對應 Action |
| 組件 | 角色 |
|---|---|
| ActionServlet | 控制器 Servlet,接收所有請求 |
| Action Class | 封裝具體業務邏輯 |
| ActionForm | 表單資料載體,支援驗證 |
| ActionMapping | 請求 URI 與 Action 類的映射 |
| ActionForward | 邏輯視圖名稱對應實際路徑 |
| ActionError | 錯誤訊息收集與回顯 |
| Struts TagLib | JSP 自定義標籤庫 |
mapping.findForward() 使用。<action path="/createBook" type="BookAction" name="bookForm" scope="request" input="/CreateBook.jsp"> 定義請求路徑、Action 類、Form Bean、作用域、輸入頁與轉發邏輯。<form-bean name="bookForm" type="BookForm"/> 宣告表單 Bean 類別與名稱。servlet.findDataSource("key") 取得連線。scope 檢查/建立 ActionForm 實例,調用 reset()、setter() 填充資料。validate="true" 時調用 validate(),失敗回 input 頁。Struts 將 MVC 模式具體化為可配置、可擴展的框架,雖已被 Spring MVC 取代,但其設計思想仍具參考價值。