Struts 1 作為早期 MVC 框架代表,雖已停止維護,但大量存量系統仍在運行。理解其配置機制有助於維護遺留項目及理解現代框架演進。
<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>
關鍵點:
config 指定主配置文件,子應用用 config/sub1debug/detail 控制啟動日志詳盡度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>
<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>
<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" %> -->
<!ELEMENT struts-config (
data-sources?,
form-beans?,
global-exceptions?,
global-forwards?,
action-mappings,
controller?,
message-resources*,
plug-in?
)>
<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();
<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>
<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>
<global-forwards>
<forward name="login" path="/login.jsp" redirect="false"/>
<forward name="home" path="/Home.do" redirect="true"/>
</global-forwards>
<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>
<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"/>
<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 存在以下痛點:
遷移路徑:Struts 1 → Struts 2 → Spring MVC,或直接重寫為 Spring Boot + Thymeleaf/Freemarker/前端分離架構。