---
title: "struts2与spring2集成"
author: "Allen.CD.China"
date: "2007-10-03T17:12:27Z"
tags: []
categories: ["企业架构"]
slug: "struts2-spring2"
draft: false
lastmod: "2007-10-03T17:12:27Z"
id: 128999
url: "https://allenj2ee.iteye.com/blog/128999"
---

在Eclipse下建立一个Dynamic Web Application。

从struts2.0.6的lib目录中复制下面的库文件到WEB-INF/lib目录下：  
commons-logging-1.1.jar  
freemarker-2.3.8.jar  
ognl-2.6.9.jar  
struts-api-2.0.6.jar  
struts-core-2.0.6.jar  
struts-spring-plugin-2.0.6.jar  
xwork-2.0.0.jar

从spring中lib目录中复制下面的库文件到WEB-INF/lib目录下：  
spring.jar

修改web.xml，增加一个struts的分派器filter，映射所有的url-pattern，再增加一个spring的ContextLoaderListener监听器。修改后的内容如下：  
&lt;!--l version="1.0" encoding="UTF-8--&gt;

xml 代码

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;web-app id="WebApp\_ID" version="2.4"
03. xmlns="http://java.sun.com/xml/ns/j2ee"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app\_2\_4.xsd"&gt;
06. &lt;display-name&gt;struts2tutorial&lt;/display-name&gt;
07. &lt;filter&gt;
08. &lt;filter-name&gt;struts2&lt;/filter-name&gt;
09. &lt;filter-class&gt;org.apache.struts2.dispatcher.FilterDispatcher&lt;/filter-class&gt;
10. &lt;/filter&gt;
11. &lt;filter-mapping&gt;
12. &lt;filter-name&gt;struts2&lt;/filter-name&gt;
13. &lt;url-pattern&gt;\*.action&lt;/url-pattern&gt;
14. &lt;/filter-mapping&gt;
15. &lt;welcome-file-list&gt;
16. &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
17. &lt;/welcome-file-list&gt;
18. &lt;listener&gt;
19. &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
20. &lt;/listener&gt;
21. &lt;/web-app&gt;

写一个简单的Action，HelloWorld：

java 代码

01. package tutorial;
02. import com.opensymphony.xwork2.ActionSupport;
03. public class HelloWorld extends ActionSupport {
04. public static final String MESSAGE = "Struts is up and running ...";
05. public String execute() throws Exception {
06. setMessage(MESSAGE);
07. return SUCCESS;
08. }
09. private String message;
10. public void setMessage(String message){
11. this.message = message;
12. }
13. public String getMessage() {
14. return message;
15. }
16. }

在源文件路径下（项目的src目录）增加struts.xml配置action。这个文件是集成spring的关键所在，这里面描述有如何将spring2集成到struts2的相关信息：  
&lt;!--CTYPE struts PUBLIC&lt;br--&gt;

xml 代码

01. &lt;!DOCTYPE struts PUBLIC
02. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
03. "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
04. &lt;struts&gt;
05. &lt;!-- 下面这句表明对象是由spring负责产生的.加上这句后,struts会产生让spring负责&lt;/span&gt;&lt;/li&gt; &lt;li class=""&gt;&lt;span class="tag"&gt;         产生bean,如果spring不能产生bean,则由struts自己产生.也可以在struts.properties&lt;/span&gt;&lt;/li&gt; &lt;li class=""&gt;&lt;span class="tag"&gt;         文件内定义这个属性.--&gt;
06. &lt;constant name="objectFactory" ＆#118alue="spring"&gt;&lt;/constant&gt;
07. &lt;package name="struts2tutoial" extends="struts-default" namespace="/"&gt;
08. &lt;!-- 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id&lt;/span&gt;&lt;/li&gt; &lt;li class="alt"&gt;&lt;span&gt;             详细信息请看下面的spring的bean配置文件applicationContext.xml --&gt;
09. &lt;action name="HelloWorld" class="helloWorld"&gt;
10. &lt;result&gt;/helloWorld.jsp&lt;/result&gt;
11. &lt;/action&gt;
12. &lt;!-- Add your actions here --&gt;
13. &lt;/package&gt;
14. &lt;/struts&gt;

在WEB-INF/目录下增加spring的bean配置文件applicationContext.xml：  
&lt;!--l version="1.0" encoding="UTF-8--&gt;

xml 代码

1. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
2. &lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"&gt;
3. &lt;beans&gt;
4. &lt;bean id="helloWorld" class="tutorial.HelloWorld"&gt;&lt;/bean&gt;
5. &lt;/beans&gt;

配置很简单，只有一个bean。

最后，在WebContent目录下增加helloWorld.jsp：  
&lt;!--taglib prefix="s" uri="/struts-tags"--&gt;

xml 代码

1. &lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
2. &lt;html&gt;
3. &lt;head&gt;
4. &lt;title&gt;Hello World!&lt;/title&gt;
5. &lt;/head&gt;
6. &lt;body&gt;
7. &lt;h2&gt;&lt;s:property ＆#118alue="message" /&gt;&lt;/h2&gt;
8. &lt;/body&gt;
9. &lt;/html&gt;