1. 用戶提交請求 → *.do
2. ActionServlet (Front Controller) 接收
3. 委託 RequestProcessor (模板方法模式) 處理
4. RequestProcessor:
a. 根據 path 查找 ActionMapping (struts-config.xml 映射)
b. 獲取/創建 ActionForm → BeanUtils 自動填充請求參數
c. 調用 ActionForm.validate() → ActionErrors? → 返回 input 頁面
d. 獲取/創建 Action 實例 (單例, 線程不安全!)
e. 調用 Action.execute(mapping, form, request, response)
f. 獲得 ActionForward (邏輯視圖名)
g. mapping.findForward() 解析物理路徑
5. RequestDispatcher.forward() → JSP/Tiles 渲染
6. 響應返回瀏覽器
<action> 所有屬性:path, type, name, scope, validate, forwards, exceptions。ActionConfig,提供 findForward(String name) 解析邏輯視圖。request.getAttribute(Globals.MAPPING_KEY) 獲取。核心方法 (可覆寫定制):
processMapping()、processActionForm()、processValidate()、processActionCreate()、processActionPerform()、processForward()。reset(ActionMapping, HttpServletRequest):請求開始前重置默認值。validate(ActionMapping, HttpServletRequest):返回 ActionErrors,框架自動 saveErrors(request, errors) 並轉發至 input 頁面。struts-config.xml 聲明 <form-property>,無需 Java 類,適合動態/原型開發。ActionErrors 為 List<ActionError> 容器,支援全局錯誤 (GLOBAL_ERROR) 與字段級錯誤。<html:errors/> 或 <html:messages/> 在 JSP 顯示。ActionError("key", args...) 關聯資源文件。| 類型 | 分發依據 | 適用場景 |
|---|---|---|
| DispatchAction | 請求參數 method=xxx | CRUD 合一 Action |
| LookupDispatchAction | 提交按鈕 name (資源鍵值) | 國際化按鈕、安全分發 |
| MappingDispatchAction | <action parameter="method"> | 配置驅動分發 |
<html:form action="/logon" focus="username" onsubmit="return validateLogonForm(this)">:客戶端 JS 驗證 (<html:javascript> 生成) + 服務端雙重保障。<bean:message key="prompt.username"/>:I18N 标籤輸出標籤文本。<html:password redisplay="false"/>:密碼不回顯,安全最佳實踐。<form-beans>
<form-bean name="logonForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username" type="java.lang.String"/>
<form-property name="password" type="java.lang.String"/>
</form-bean>
<form-bean name="subscriptionForm" type="com.app.SubscriptionForm"/> <!-- 傳統 ActionForm -->
</form-beans>
<action-mappings>
<action path="/logon"
type="com.app.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp"> <!-- 驗證失敗返回頁 -->
<forward name="success" path="/mainMenu.jsp"/>
<forward name="failure" path="/logon.jsp"/>
</action>
</action-mappings>
DynaValidatorForm 通過 PropertyUtils.getSimpleProperty() 取值 (非強轉)。saveErrors(request, errors) + return mapping.getInputForward()。HttpSession,清理請求/會話層 FormBean,轉發成功頁。arithmetic 拋 ArithmeticException,expired 拋 ExpiredPasswordException,配合 <global-exceptions> 驗證框架機制。public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (host == null || host.length() == 0)
errors.add("host", new ActionError("error.host.required"));
if (username == null || username.length() == 0)
errors.add("username", new ActionError("error.username.required"));
if (password == null || password.length() == 0)
errors.add("password", new ActionError("error.password.required"));
if (type == null || type.length() == 0)
errors.add("type", new ActionError("error.type.required"));
else if (!"imap".equals(type) && !"pop3".equals(type))
errors.add("type", new ActionError("error.type.invalid", type));
return errors;
}
ActionError 使用資源鍵值,支援多語言部署。mapping.getScope() == "session" 時手動 session.removeAttribute(),防止內存洩漏與數據殘留。Struts 工作流程以 RequestProcessor 為骨干,串聯 ActionMapping、ActionForm、Action、ActionForward 核心組件。深入理解各組件職責、交互協議、線程模型、擴展點,是編寫高質量 Struts 應用、排查疑難雜癥、平滑遷移 Struts 2/Spring MVC 的關鍵。