Struts1 作為早期經典 MVC 框架,雖已退出歷史舞台,但其「中央控制器 + ActionForm + Action + ActionForward」架構思想深刻影響後續框架設計。本文系統梳理其運行流程與整合配置要點。
web.xml,ActionServlet(中央控制器)作為 load-on-startup Servlet 啟動,讀取 struts-config.xml 初始化各模塊對象*.do 請求被 ActionServlet 攔截ActionForm 實例ActionMapping 找到目標 Action,將 ActionForm 傳入 execute()Action.execute() 執行業務邏輯(調用 Service/DAO),返回 ActionForwardActionForward 封裝跳轉邏輯名(如 "success")ActionForward 在配置中找到對應 JSP 路徑| 組件 | 角色 | 關鍵點 |
|---|---|---|
| ActionServlet | 中央控制器 | 單例,生命週期贯穿應用,線程不安全需注意 |
| ActionForm | 表單數據載體 | 按 name 匹配自動填充,需提供無參構造器 + getter/setter |
| Action | 業務處理器 | 每個請求對應一個 execute(),返回 ActionForward |
| ActionForward | 跳轉標識 | 邏輯名映射物理路徑,支援重定向/轉發 |
| struts-config.xml | 核心配置 | 定義 FormBean、ActionMapping、Forward、DataSource 等 |
核心:struts-core.jar、struts-taglib.jar、commons-beanutils.jar、commons-chain.jar、commons-digester.jar、commons-logging.jar、commons-validator.jar、oro.jar
<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>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"https://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="welcomeForm" type="com.example.form.WelcomeForm"/>
</form-beans>
<action-mappings>
<action path="/welcome"
type="com.example.action.WelcomeAction"
name="welcomeForm"
scope="request"
validate="true"
input="/welcome.jsp">
<forward name="success" path="/WEB-INF/page/welcome.jsp"/>
</action>
</action-mappings>
<message-resources parameter="com.example.resources.ApplicationResources"/>
</struts-config>
public class WelcomeForm extends ActionForm {
private String username;
// getter/setter 省略
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
username = ""; // 重置表單
}
@Override
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (username == null || username.trim().isEmpty()) {
errors.add("username", new ActionMessage("error.username.required"));
}
return errors;
}
}
public class WelcomeAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
WelcomeForm wf = (WelcomeForm) form;
request.setAttribute("username", wf.getUsername());
return mapping.findForward("success");
}
}
<%@ taglib uri="https://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="https://struts.apache.org/tags-html" prefix="html" %>
<html:form action="/welcome">
<html:text property="username"/>
<html:errors property="username"/>
<html:submit/>
</html:form>
<!-- welcome.jsp -->
歡迎您,<bean:write name="username"/>!
URL:https://localhost:8080/項目名/welcome.do → 跳轉顯示歡迎頁
execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
mapping:封裝配置信息,提供 findForward()form:已填充數據的 ActionFormrequest/response:原生 Servlet API