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

Struts 1.x 系列教程(24):配置檔總覽與核心元素詳解

     編輯:Java 相關知識

前言

Struts 1.x 應用至少依賴兩個配置檔:web.xml 安裝框架入口,struts-config.xml 定義框架內部資源(FormBean、Action 對應、全域轉向、例外處理、控制器參數、訊息資源、外掛模組)。本文系統整理這七大核心配置區塊,並附上標準範例。

一、在 web.xml 安裝 Struts

Struts 入口為 ActionServlet,於首次請求時初始化並讀取 struts-config.xml。web.xml 需宣告此 Servlet 並指定配置檔位置:

<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>

二、struts-config.xml 根結構

所有配置置於 <struts-config> 根元素下,七大子元素:

  1. <form-beans> - FormBean 定義
  2. <action-mappings> - Action 路徑對應
  3. <global-forwards> - 全域轉向
  4. <global-exceptions> - 全域例外處理
  5. <controller> - 控制器參數
  6. <message-resources> - 訊息資源(屬性檔)
  7. <plug-in> - 外掛模組

三、FormBean 與 Action 對應(核心雙雄)

FormBean 定義

<form-beans>
    <form-bean name="myForm" type="actionform.MyActionForm" />
</form-beans>
  • name:邏輯名稱,供 Action 參照。
  • type:完整類別名,需繼承 ActionForm 或使用 DynaActionForm。

Action 對應

<action-mappings>
    <action name="myForm"
            path="/myaction"
            scope="request"
            validate="true"
            type="action.MyAction"
            input="/mystruts/error.jsp">
        <forward name="success" path="/mystruts/success.jsp" />
    </action>
</action-mappings>
  • path:請求路徑(對應 *.do)。
  • name:綁定的 FormBean 名稱。
  • scope:request 或 session。
  • validate:是否自動呼叫 FormBean validate()。
  • input:驗證失敗時返回的頁面。
  • <forward>:本地轉向定義(成功、失敗等)。

四、全域轉向 <global-forwards>

可被所有 Action、例外處理、標籤(如 <html:link>)存取:

<global-forwards>
    <forward name="myforward" path="/mystruts/newProduct.jsp" />
</global-forwards>

五、例外處理配置

全域例外 <global-exceptions>

<global-exceptions>
    <exception key="error.name.blank"
               type="java.lang.Exception"
               path="/error.jsp" />
</global-exceptions>
  • key:屬性檔錯誤訊息鍵值。
  • type:攔截的例外類別。
  • path:導向的錯誤頁面。
  • 亦可用 handler 指定自訂處理類(需繼承 ExceptionHandler)。

區域例外

置於 <action> 內部,優先級高於全域。

六、控制器參數 <controller>

<controller maxFileSize="2M" contentType="text/html" nocache="false" />
  • maxFileSize:上傳檔案大小上限(K/M/G)。
  • nocache:true 禁用快取,預設 true。
  • contentType:預設回應內容型態。

七、訊息資源 <message-resources>

<message-resources parameter="struts.application" key="global" />
  • parameter:屬性檔基底名稱(對應 WEB-INF/classes/struts/application.properties)。
  • key:資源束識別碼,使用時需在標籤指定 bundle="global"。

應用範例:<bean:message key="key.msg.submit" bundle="global"/>、驗證錯誤 errors.add("name", new ActionMessage("error.name.blank")) 配合 <html:errors property="name" bundle="global"/>。

八、外掛模組 <plug-in>

如 Validator、Tiles:

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
                  value="/WEB-INF/validator-rules.xml,/WEB-INF/validator.xml" />
</plug-in>
  • className:外掛實作類別。
  • <set-property>:傳遞初始化參數。

總結

掌握這七大配置區塊,即能完整掌控 Struts 1.x 應用的資料流向、驗證、國際化、版面佈局與擴充機制。實務上建議將大型專案的 struts-config.xml 拆分為多個模組檔案並以 <include> 整合(Struts 1.1+ 支援)。

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