---
title: "集成Spring与Struts"
author: "justin8335"
date: "2007-09-28T17:00:33Z"
tags: []
slug: "spring-struts"
draft: false
lastmod: "2007-09-28T17:00:33Z"
id: 127964
url: "https://justinc.iteye.com/blog/127964"
---

Struts是一个Web层框架，需要考虑如何获得业务逻辑接口。一个较好的设计就是首先设计一个BaseAction，其中定义获得业务逻辑接口的方法，其他所有Action继承了BaseAction，方便地调用业务逻辑接口。

java 代码

 

1. public class BaseAction extends Action {
2. private static final UserService userService = new UserServiceImpl();
3. public UserService getUserService() {
4. return userService;
5. }
6. }

这种以静态的方法持有业务逻辑接口的引用的设计并不优雅。  
使用Spring的IOC来配置管理这些逻辑组件,则完全可以实现一个优雅的多层应用程序。

在Spring中有两种方法集成Struts

（1）通过一个Spring提供的一个Struts插件来初始化Ioc容器。

xml 代码

 

1. &lt;plug-in className="org.springframework.web.struts.ContextLoaderPlugIn" &gt;
2. &lt;set-property property="contextConfigLocation"
3. value="/WEB-INF/applicationContext.xml"/&gt;
4. plug-in&gt;

注意：我们要将Spring的配置文件放置在/WEB-INF/目录下，并且如果不是使用Spring的默认命名（applicationContext.xml），  
就必须在以上位置添加描述。否则，会出现404错误。在这里，配置文件是使用默认的名称，但我添加了描述。

然后，我们将BaseAction修改，使其继承ActionSupport（org.springframework.web.struts.ActionSupport）。

java 代码

 

1. public class BaseAction extends ActionSupport {
2. public UserService getUserService(){
3. return (UserService)getWebApplicationContext().getBean("userService");
4. }
5. }

ActionSupport是Spring提供的一个继承自Action的抽象类。  
它有一个很重要的方法`getWebApplicationContext()，通过该方法，我们可以获得当前的Ioc容器的引用ApplicationContext，一旦获得了该引用，我们就可以获得Spring的IOC容器中的所有的Bean。 此外，我们还要在Spring的配置文件applicationContext.xml中配置bean(userService)`

xml 代码

1. xml version="1.0" encoding="UTF-8"?&gt;
2. &lt;beans
3. xmlns="http://www.springframework.org/schema/beans"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"&gt;
6. &lt;bean id="userService" class="org.justin.service.impl.UserServiceImpl" /&gt;
7. beans&gt;

`该方法的好处是，只要合理的抽象出一个基类BaseAction,只要修改BaseAction使其继承ActionSupport就可以实现Spring与Struts的集成，而无需修改任何Action。 （2）另一种方法就是就是将Struts中的所有Action都作为SpringIOC容器中的Bean来管理。 通过Spring提供的DelegatingRequestProcessor来替代Struts的默认派发器，以便让Spring能截获派发给Action的请求。 这种方式下，业务逻辑组件通过依赖注入的方式在Spring的Ioc容器中就配置完毕了。 我们往struts-config.xml中添加一条 xml 代码 <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />  xml 代码   <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn" >     <set-property property="contextConfigLocation"        value="/WEB-INF/applicationContext.xml"/>   </plug-in>   Spring提供的Struts PlugIn仍负责启动Spring容器，而 DelegatingRequestProcessor可以让Spring接管请求， 从而将请求派发到Ioc容器管理的Action实例。因此，我们还必须在Spring的配置文件中声明每一个Action类，其name属性 和Struts配置文件中的path要完全一致。 xml 代码   <bean name="/login" class="struts.action.LoginAction">        <property name="userService" ref="userService"></property>   </bean>      <bean name="/logout" class="struts.action.LogoutAction">        <property name="userService" ref="userService"></property>   </bean>   其实在struts-config.xml中我们就不需要在配置action的type属性了。 这种配置最大的不便就是所有的Action必须同时在Struts和Spring中各配置一次。`