struts-config.xml 是 Struts 應用的「總開關」,定義 FormBean、全局轉發、動作映射三大核心要素。本文解析實戰配置,並給出生產級模組化拆分方案。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"https://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- 1. FormBean 註冊:name 對應 Action 映射的 name 屬性 -->
<form-beans>
<form-bean name="examineeActionForm" type="tclcc.tester.business.ExamineeActionForm" />
<form-bean name="maintainForm" type="tclcc.tester.business.maitain.MaintainForm" />
<form-bean name="addPlanForm" type="tclcc.tester.business.maitain.AddPlanForm" />
</form-beans>
<!-- 2. 全局轉發:不綁定具體 Action,全應用可用 -->
<global-forwards>
<forward name="veiwtrainplan" path="/veiwtrainplan.jsp" />
</global-forwards>
<!-- 3. 動作映射:核心路由表 -->
<action-mappings>
<!-- 考生模塊 -->
<action name="examineeActionForm" path="/admin/showMain"
type="tclcc.tester.business.ShowMain">
<forward name="success" path="/admin/examinee/main.jsp" />
</action>
<action input="/admin/examinee/addExaminee.jsp"
name="examineeActionForm" path="/admin/saveAddExaminee"
type="tclcc.tester.business.SaveAddExamineeAction" validate="true">
<forward name="success" path="/admin/showMain.do" /> <!-- 重定向到另一 Action -->
</action>
<action name="examineeActionForm" path="/admin/addExaminee"
type="tclcc.tester.business.AddExamineeAction">
<forward name="success" path="/admin/examinee/addExaminee.jsp" />
</action>
<!-- 計劃維護模塊 (文件上傳核心) -->
<action input="/admin/maintain/planManage.jsp"
name="maintainForm" path="/maintainAction" scope="request"
type="tclcc.tester.business.maitain.MaintainAction" validate="true">
<forward name="trainplanCreated" path="/veiwtrainplan.jsp" />
</action>
<action name="maintainForm" path="/initAction" scope="request"
type="tclcc.tester.business.maitain.InitAction" validate="true">
<forward name="maintain" path="/admin/maintain/planManage.jsp" />
</action>
</action-mappings>
<!-- 4. 其他可配置區塊(本文件未使用) -->
<!-- <message-resources parameter="ApplicationResources"/> -->
<!-- <plug-in className="org.apache.struts.validator.ValidatorPlugIn">...</plug-in> -->
<!-- <data-sources>...</data-sources> -->
</struts-config>
| 屬性 | 含義 | 關鍵點 |
|---|---|---|
path | 請求路徑 (不含 .do) | 唯一標識,/maintainAction 對應 /maintainAction.do |
type | Action 類全限定名 | 容器啟動時實例化(單例),需無參構造函數 |
name | FormBean 邏輯名 | 對應 <form-bean> 的 name,Struts 自動實例化/復用並填充 |
scope | FormBean 作用域 | request(默認):每次請求新建/復用;session:跨請求保持狀態 |
validate | 是否調用 FormBean.validate() | true:驗證失敗自動返回 input 頁面;false:跳過驗證 |
input | 驗證失敗/異常時返回頁面 | 必須與 validate="true" 配合使用 |
forward | 邏輯視圖名 → 物理路徑 | name="success" 為慣例;path 以 / 開頭表示相對 Web 應用根目錄 |
單一巨型 struts-config.xml 維護困難,建議按業務模組拆分:
<!-- 主配置 struts-config.xml -->
<struts-config>
<!-- 引入模組配置 -->
<!-- Struts 1.1+ 支援模組前綴,但較繁瑣;推薦 1.2+ 使用 <include> -->
<include file="/WEB-INF/struts-config-examinee.xml" />
<include file="/WEB-INF/struts-config-maintain.xml" />
<include file="/WEB-INF/struts-config-common.xml" />
</struts-config>
struts-config-examinee.xml:考生模塊所有 /admin/examinee/* 路徑。struts-config-maintain.xml:計劃維護模塊 /maintain/*、/initAction 等。struts-config-common.xml:全局 <form-beans>、<global-forwards>、<message-resources>、<plug-in>。| Struts 1.x | Spring MVC | 優勢 |
|---|---|---|
| XML 集中式映射 | @RequestMapping 分散式註解 | 代碼即配置,重構友好,IDE 支援跳轉 |
ActionForm 綁定 | @ModelAttribute / DTO 直接綁定 | 無需專門 Form 類,POJO 即可 |
ActionForward 字串 | 返回 String 視圖名 / ModelAndView | 類型安全,支援 RedirectView、ResponseBody |
| 單例 Action 線程安全自管 | Controller 默認單例,請求數據通過參數傳遞 | 天然線程安全,無實例變量風險 |
理解 struts-config.xml 映射機制,是閱讀遷移舊系統、設計 RESTful API 路由規範的重要基礎。