---
title: "【分享】spring3.0与struts2的整合"
author: "程序员程飞"
date: "2011-02-15T22:02:05Z"
tags: ["Bean", "Spring", "Struts", "Web", "XML"]
categories: ["编程语言"]
slug: "spring3-0-struts2"
draft: false
lastmod: "2011-02-15T22:15:46Z"
id: 907281
url: "https://chengfei.iteye.com/blog/907281"
---

个人觉得整合这两个框架的关键就在于不需要再使用ApplicationContext来配置Spring的配置文件。（废话……）

 

接下来就来解开……

 

工欲善其事，必先利其器！准备好能将两者相结合的证明（结婚对戒：需要的jar包）。

1.commons-logging.jar ，这个可以在Struts2的lib下找到。里面有两个，我使用的是附件中的，另一个没测试，估计没有问题……

2.struts2-spring-plugin-2.2.1，这个依然在Struts2的lib下能找到。

 

配置web.xml的内容：

 

```
<context-param>
 	<param-name>contextConfigLocation</param-name>
	 <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param>
 <listener> 
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener> 

```

    

 

   将Struts的业务逻辑控制器类配置在Spring的配置文件中：大家都知道Spring的核心就是一个Bean容器，用以产生对象并注入到应用程序中。所以只要把Action配置在Spring的配置文件中，并将业务处理对象做为其属性进行引用就可以方便的进行程序控制了。

      applicationContext.xml代码片断：

 

```
<!-- 用户登录的Action --> <bean name="loginAction" class="com.cf.user.action.LoginAction"> 	<property name="userDao" ref="impUserDao"></property> </bean>
```

  

     再来就是在struts.xml配置文件中配置Action时，指定&lt;action&gt;的class属性为Spring配置文件中相应bean的id或者name值。示例如下：

 

```
<package name="user" namespace="/user" extends="struts-default"> 
<action name="userlogin" class="loginAction" > <result>/login_success.jsp</result> 
</action> 
</package> 

```

  

 

     这样一个简单的登录功能就通过两者的结合实现了。具体的业务Dao自行完成！