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

Struts 2 + Spring 整合實戰:從零構建輕量級 MVC 架構

     編輯:Java 相關知識

Struts 2.0.6 GA 釋出標誌著 WebWork 正式併入 Apache 主線。本文記錄從零搭建 Struts 2 + Spring 整合項目的完整步驟,涵蓋依賴管理、核心配置、動作開發與最佳實踐,作為遷移與新項目啟動的參考模板。

項目結構與依賴

webapp/
├── src/
│   ├── main/
│   │   ├── java/tutorial/HelloWorld.java
│   │   ├── resources/struts.xml
│   │   └── webapp/
│   │       ├── WEB-INF/
│   │       │   ├── web.xml
│   │       │   ├── applicationContext.xml
│   │       │   └── lib/ (見下表)
│   │       └── helloWorld.jsp
│   └── test/
類別JAR (Struts 2.0.6)
核心struts2-core-2.0.6.jar, xwork-2.0.0.jar, struts2-api-2.0.6.jar
模板引擎freemarker-2.3.8.jar
表達式語言ognl-2.6.9.jar
日誌commons-logging-1.1.jar
Spring 整合struts2-spring-plugin-2.0.6.jar
Springspring.jar (2.5.x)

核心配置三件套

1. web.xml - 入口攔截與 Spring 上下文啟動

<web-app version="2.4" xmlns="https://java.sun.com/xml/ns/j2ee">
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

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

2. applicationContext.xml - Spring 管理 Action Bean

<beans xmlns="https://www.springframework.org/schema/beans">
  <bean id="helloWorld" class="tutorial.HelloWorld" />
</beans>

3. struts.xml - 關鍵:objectFactory=spring + class 指向 bean id

<struts>
  <constant name="struts.objectFactory" value="spring" />
  
  <package name="default" extends="struts-default" namespace="/">
    <action name="HelloWorld" class="helloWorld">  <!-- 此處寫 Spring bean id -->
      <result>/helloWorld.jsp</result>
    </action>
  </package>
</struts>

Action 開發範例

package tutorial;
import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport {
    public static final String MESSAGE = "Struts 2 + Spring 整合成功!";
    
    private String message;
    
    public String execute() {
        setMessage(MESSAGE);
        return SUCCESS;
    }
    
    // getter/setter 省略
}

視圖頁面

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head><title>Hello World</title></head>
<body>
  <h2><s:property value="message" /></h2>
</body>
</html>

關鍵整合原理解析

  1. StrutsPrepareAndExecuteFilter (2.1.3+ 替代 FilterDispatcher) 初始化 StrutsObjectFactory
  2. struts.objectFactory=spring 觸發 SpringObjectFactory 載入
  3. Action 實例化時委託 WebApplicationContext.getBean("helloWorld")
  4. Spring 完成依賴注入 (Setter/構造器/@Autowired) 後返回給 Struts
  5. Struts 執行攔截器鏈 → execute() → 結果視圖渲染

常見問題與解決

現象原因解決
ClassNotFound: FilterDispatcherStruts 2.1.3+ 已移除改用 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
Action 依賴注入失效 (null)struts.xml 中 class 寫成全限定類名必須寫 Spring bean id (如 "helloWorld")
OGNL 表達式報錯Getter/Setter 命名不規範嚴格遵循 JavaBean 規範,或使用 @Action 註解 (2.1+)

遷移建議

  • 新項目直接使用 Struts 2.5.x + Spring 5.x,註解驅動 (@Action, @Result) 替代 XML
  • 舊項目升級:保留 struts.xml,逐步引入 struts2-convention-plugin 實現約定優於配置
  • 考慮遷移至 Spring MVC:統一編程模型、更強的 REST 支持、社區活躍度更高

完整 Maven 項目代碼見附件,包含 Jetty 插件配置,mvn jetty:run 即時運行驗證。

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