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

Struts 文件上傳實戰 (九):struts-config.xml 完整配置解析與模組化建議

     編輯:Java 程式設計入門

配置文件全景

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
typeAction 類全限定名容器啟動時實例化(單例),需無參構造函數
nameFormBean 邏輯名對應 <form-bean> 的 name,Struts 自動實例化/復用並填充
scopeFormBean 作用域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>。

遷移 Spring MVC 對比

Struts 1.xSpring MVC優勢
XML 集中式映射@RequestMapping 分散式註解代碼即配置,重構友好,IDE 支援跳轉
ActionForm 綁定@ModelAttribute / DTO 直接綁定無需專門 Form 類,POJO 即可
ActionForward 字串返回 String 視圖名 / ModelAndView類型安全,支援 RedirectView、ResponseBody
單例 Action 線程安全自管Controller 默認單例,請求數據通過參數傳遞天然線程安全,無實例變量風險

理解 struts-config.xml 映射機制,是閱讀遷移舊系統、設計 RESTful API 路由規範的重要基礎。

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