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

Struts 應用配置詳解:web.xml 與 struts-config.xml 核心元素全解析

     編輯:Java 相關知識

Struts 應用通過兩個 XML 配置文件實現裝配:通用的 web.xml 與 Struts 專用的 struts-config.xml(名稱可自定義)。本文系統梳理兩文件的核心元素、屬性與配置順序,助你快速掌握 Struts 配置精髓。

一、 web.xml - Web 應用部署描述符

Web 容器啟動時讀取此文件裝載應用。根元素 <web-app> 子元素有嚴格順序:<servlet> → <servlet-mapping> → <taglib> 等。

1. 配置 ActionServlet(核心控制器)

<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>
  • 全應用僅需一個 ActionServlet(Servlet 本身多線程)
  • config 參數指定 Struts 配置文件路徑

2. 歡迎文件列表

<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

3. 錯誤頁面配置

<error-page>
 <error-code>404</error-code>
 <location>/error/404.jsp</location>
</error-page>
<error-page>
 <exception-type>java.lang.Exception</exception-type>
 <location>/error/exception.jsp</location>
</error-page>

4. 標簽庫聲明

<taglib>
 <taglib-uri>/tags/struts-bean</taglib-uri>
 <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

二、 struts-config.xml - Struts 核心配置

啟動時由 Digester 組件解析,生成 org.apache.struts.config 包下的 JavaBean 對象樹,核心為 ModuleConfig。根元素 <struts-config> 子元素順序固定(按 DTD):

data-sources? → form-beans? → global-exceptions? → global-forwards? → 
action-mappings? → controller? → message-resources? → plug-in*

1. <data-sources> - 數據源配置

<data-sources>
 <data-source key="myDB" type="org.apache.commons.dbcp.BasicDataSource">
  <set-property property="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
  <set-property property="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
  <set-property property="username" value="scott"/>
  <set-property property="password" value="tiger"/>
 </data-source>
</data-sources>

Action 中通過 getDataSource(request, "myDB") 獲取。

2. <form-beans> - ActionForm 註冊

<form-beans>
 <form-bean name="loginForm" type="com.example.form.LoginForm"/>
</form-beans>
  • name:唯一標識,供 Action 引用
  • type:全限定類名

3. <global-exceptions> - 全局異常映射

<global-exceptions>
 <exception key="error.database" type="java.sql.SQLException" path="/error/db.jsp"/>
</global-exceptions>

4. <global-forwards> - 全局轉發

<global-forwards>
 <forward name="login" path="/login.jsp" redirect="false"/>
</global-forwards>
  • redirect="true":重定向(瀏覽器地址變化)
  • redirect="false":請求轉發(地址不變)

5. <action-mappings> - Action 映射(核心)

<action-mappings>
 <action path="/login" type="com.example.action.LoginAction"
  name="loginForm" scope="request" validate="true" input="/login.jsp">
  <forward name="success" path="/welcome.jsp"/>
  <forward name="failure" path="/login.jsp" redirect="true"/>
  <exception key="error.auth" type="com.example.AuthException" path="/error/auth.jsp"/>
 </action>
</action-mappings>

關鍵屬性:

  • path:請求路徑(必須以 / 開頭)
  • type:Action 實現類全名
  • name:關聯的 Form Bean 名稱
  • scope:Form 存活範圍,默認 session
  • validate:是否調用 Form 的 validate()
  • input:驗證失敗返回頁面

6. <controller> - 控制器參數

<controller bufferSize="4096" contentType="text/html;charset=UTF-8" locale="true" nocache="true"/>

7. <message-resources> - 國際化資源

<message-resources parameter="com.example.resources.ApplicationResources"/>

8. <plug-in> - 插件配置(如 Validator、Tiles)

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
 <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

三、 多模塊應用配置

  • 共享單個 ActionServlet 實例
  • 每個模塊擁有獨立 struts-config.xml
  • 在 web.xml 中為 ActionServlet 添加多個 init-param(config/模塊前綴)
  • 模塊間跳轉使用全局轉發

四、 實戰提示

現代 IDE(Eclipse/MyEclipse/IntelliJ)提供 Struts 插件,新建專案時自動生成配置骨架,僅需填寫屬性值。理解原理才能在疑難雜症時定位問題。

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