萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> JSP 程式設計

Struts 1.x 核心配置全解析:web.xml、struts-config.xml 與標籤庫實戰指南

Struts 1.x 架構回顧

Struts 1 作為早期 MVC 框架代表,雖已停止維護,但大量存量系統仍在運行。理解其配置機制有助於維護遺留項目及理解現代框架演進。

一、web.xml 核心配置

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>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</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,支持多線程
  • config 指定主配置文件,子應用用 config/sub1
  • debug/detail 控制啟動日志詳盡度

2. 歡迎頁面與 Action 映射技巧

web.xml 不支持直接映射 Action 為 welcome-file,變通方案:

<!-- struts-config.xml -->
<global-forwards>
    <forward name="welcome" path="/HelloWorld.do"/>
</global-forwards>

<!-- welcome.jsp -->
<%@ page contentType="text/html;charset=UTF-8" %>
<% response.sendRedirect(request.getContextPath() + "/HelloWorld.do"); %>

<!-- web.xml -->
<welcome-file-list>
    <welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>

3. 錯誤頁面配置

<error-page>
    <error-code>404</error-code>
    <location>/common/404.jsp</location>
</error-page>
<error-page>
    <error-code>500</error-code>
    <location>/common/500.jsp</location>
</error-page>
<error-page>
    <exception-type>java.io.IOException</exception-type>
    <location>/common/system_ioerror.jsp</location>
</error-page>

4. 標籤庫聲明

<taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
    <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<!-- JSP 頁面引用:<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> -->

二、struts-config.xml 結構詳解

根元素 DTD 順序(順序敏感)

<!ELEMENT struts-config (
    data-sources?, 
    form-beans?, 
    global-exceptions?, 
    global-forwards?, 
    action-mappings, 
    controller?, 
    message-resources*, 
    plug-in?
)>

1. data-sources:數據源配置

<data-sources>
    <data-source key="default" type="org.apache.commons.dbcp.BasicDataSource">
        <set-property property="driverClassName" value="com.mysql.jdbc.Driver"/>
        <set-property property="url" value="jdbc:mysql://localhost:3306/mydb"/>
        <set-property property="username" value="root"/>
        <set-property property="password" value="123456"/>
    </data-source>
</data-sources>

<!-- Action 中獲取 -->
DataSource ds = getDataSource(request, "default");
Connection conn = ds.getConnection();

2. form-beans:ActionForm 註冊

<form-beans>
    <form-bean name="loginForm" type="com.example.form.LoginForm"/>
    <!-- 動態 Form Bean -->
    <form-bean name="dynamicForm" 
              type="org.apache.struts.validator.DynaValidatorForm">
        <form-property name="username" type="java.lang.String" initial=""/>
        <form-property name="age" type="java.lang.Integer" initial="0"/>
    </form-bean>
</form-beans>

3. global-exceptions:全局異常映射

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

4. global-forwards:全局轉發

<global-forwards>
    <forward name="login" path="/login.jsp" redirect="false"/>
    <forward name="home" path="/Home.do" redirect="true"/>
</global-forwards>

5. action-mappings:核心業務映射

<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"/>
        <exception key="error.auth" type="com.example.AuthException" path="/login.jsp"/>
    </action>
    
    <!-- 無 Form Bean 的簡單 Action -->
    <action path="/logout" 
            type="com.example.action.LogoutAction">
        <forward name="success" path="/login.jsp" redirect="true"/>
    </action>
</action-mappings>

6. controller & message-resources

<controller 
    processorClass="org.apache.struts.action.RequestProcessor" 
    tempDir="work" 
    contentType="text/html;charset=UTF-8"
    locale="true"
    nocache="true"/>

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

7. 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>
<plug-in className="org.apache.struts.tiles.TilesPlugin">
    <set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml"/>
    <set-property property="moduleAware" value="true"/>
</plug-in>

三、遷移建議

新項目**強烈建議**直接使用 Spring MVC / Spring Boot,Struts 1 存在以下痛點:

  • ActionForm 導致大量樣板代碼
  • 配置文件龐大、易出錯、缺乏類型安全
  • 無 RESTful 支持、異步處理困難
  • 社區已停止維护,安全漏洞無修復

遷移路徑:Struts 1 → Struts 2 → Spring MVC,或直接重寫為 Spring Boot + Thymeleaf/Freemarker/前端分離架構。

copyright © 萬盛學電腦網 all rights reserved