---
title: "整合 Struts 和 Spring"
author: "black_angle"
date: "2008-04-11T10:52:00Z"
tags: []
slug: "struts-spring"
draft: false
lastmod: "2008-04-11T10:52:00Z"
id: 260737
url: "https://black-angle.iteye.com/blog/260737"
---

[**整合**]() **Struts 和 Spring：三种方式:&lt;o:p&gt;&lt;/o:p&gt;**

**１：**使用 Spring的`ActionSupport` 类整合 Structs２：用 Spring 的 `DelegatingRequestProcessor` 覆盖 Struts 的 `RequestProcessor３：`将 Struts Action 管理委托给 Spring 框架 &lt;o:p&gt;&lt;/o:p&gt;

无论您使用哪种技术，都需要使用 Spring 的 ContextLoaderPlugin 为 Struts 的 ActionServlet 装载 Spring 应用程序环境。就像添加任何其他插件一样，简单地向您的 struts-config.xml 文件添加该插件，通过加载吧spring的配置文件加载进来:如下所示：&lt;o:p&gt;&lt;/o:p&gt;

&lt;plug-in className=&lt;o:p&gt;&lt;/o:p&gt;

  "org.springframework.web.struts.ContextLoaderPlugIn"&gt;&lt;o:p&gt;&lt;/o:p&gt;

    &lt;set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/&gt;&lt;o:p&gt;&lt;/o:p&gt;

 &lt;/plug-in&gt;&lt;o:p&gt;&lt;/o:p&gt;

[**窍门**]() **1. 使用 Spring 的 ActionSupport(**将struts和spring耦合在一起了**)&lt;o:p&gt;&lt;/o:p&gt;**

`org.springframework.web.struts.ActionSupport`类提供了一个 `getWebApplicationContext()` 方法。您所做的只是从 Spring 的 `ActionSupport` 而不是 Struts `Action` 类扩展您的动作，如public class SearchSubmit extends ActionSupport {  //下面execute方法里的内容,将struts和spring耦合在一起了&lt;o:p&gt;&lt;/o:p&gt;

```
ApplicationContext ctx = getWebApplicationContext();  <o:p></o:p>
```

```
    BookService bookService =  (BookService) ctx.getBean("bookService");<o:p></o:p>
```

```
}//不继承ActionSupport;这里换成直接自己通过各种各种ApplicationContext来读取spring的bean定义//文件(这种情况不需要上面的< plug-in>标签也可以)<o:p></o:p>
```

[**窍门**]() **2. 覆盖 RequestProcessor&lt;o:p&gt;&lt;/o:p&gt;**

```
org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序，<controller processorClass="org.springframework.web.struts. DelegatingRequestProcessor"/>下一步是在我的 Spring 配置文件中注册该动作，<o:p></o:p>
```

```
<beans><o:p></o:p>
```

```
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
```

```
  <bean name="/searchSubmit" class="ca.nexcel.books.actions.SearchSubmit"> |(1)<o:p></o:p>
```

```
     <property name="bookService"><o:p></o:p>
```

```
        <ref bean="bookService"/><o:p></o:p>
```

```
     </property><o:p></o:p>
```

```
  </bean><o:p></o:p>
```

```
</beans><o:p></o:p>
```

```
注意：在 (1) 处，我使用名称属性注册了一个 bean，以匹配 struts-config 动作映射名称。SearchSubmit 动作揭示了一个 JavaBean 属性，允许 Spring 在运行时填充属性，SearchSubmit类就是Action里面定义了 private BookService bookService的get/set方法.<o:p></o:p>
```

```
缺点: 如果您使用一个不同的 RequestProcessor，如tiles的processor。则需要手动整合 Spring 的 DelegatingRequestProcessor。<o:p></o:p>
```

[**窍门**]() **3.** **将****动****作管理委托****给** **Spring**&lt;o:p&gt;&lt;/o:p&gt;

```
将 Strut 动作管理委托给 Spring。您可以通过在 struts-config 动作映射中注册一个代理来实现。代理负责在 Spring 环境中查找 Struts 动作。由于动作在 Spring 的控制之下，所以它可以填充动作的 JavaBean 属性，并为应用诸如 Spring 的 AOP 拦截器之类的特性带来了可能。Action类和上面那种情况的类一样<o:p></o:p>
```

```
<action    path="/searchSubmit" <o:p></o:p>
```

```
             type="org.springframework.web.struts.DelegatingActionProxy" |(1)<o:p></o:p>
```

```
             input="/searchEntry.do"<o:p></o:p>
```

```
             validate="true"<o:p></o:p>
```

```
             name="searchForm"><o:p></o:p>
```

```
             <forward name="success" path="/WEB-INF/pages/detail.jsp"/><o:p></o:p>
```

```
             <forward name="failure" path="/WEB-INF/pages/search.jsp"/><o:p></o:p>
```

```
    </action>  <o:p></o:p>
```

```
是一个典型的 struts-config.xml 文件，只有一个小小的差别。它注册 Spring 代理类的名称，而不是声明动作的类名，如（1）处所示。DelegatingActionProxy 类使用动作映射名称查找 Spring 环境中的动作。这就是我们使用 ContextLoaderPlugIn 声明的环境。<o:p></o:p>
```

```
<beans><o:p></o:p>
```

```
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/><o:p></o:p>
```

```
  <bean name="/searchSubmit"   class="ca.nexcel.books.actions.SearchSubmit"><o:p></o:p>
```

```
     <property name="bookService"><o:p></o:p>
```

```
        <ref bean="bookService"/><o:p></o:p>
```

```
     </property><o:p></o:p>
```

```
  </bean> <o:p></o:p>
```

```
*Spring Bean Name与Struts Action Path相关联，当Struts加载对应的Action时，DelegatingActionProxy就根据传入的path属性，在Spring Context寻找对应bean，并将其实例返回给Struts。<o:p></o:p>
```

动作委托的优点不止如此。一旦让 Spring 控制您的 Struts