---
title: "spring与struts集成方案二"
author: "whp0731"
date: "2009-03-31T11:31:16Z"
tags: ["AOP", "Bean", "IOC", "Spring", "Struts"]
slug: "spring-struts"
draft: false
lastmod: "2009-03-31T11:31:16Z"
id: 358192
url: "https://whp0731.iteye.com/blog/358192"
---

spring+struts的集成(第二种集成方案)  
一、原理

将业务逻辑对象通过spring注入到Action中，从而避免了在Action类中的直接代码查询

![](/upload/attachment/89343/08b53bc2-2609-3ff4-8e9a-ad95361ca08b.jpg)

 

可以看出，在这个方案中action是通过spring的IOC来创建的

1、spring和struts依赖库配置  
 * 配置struts  
  --拷贝struts类库和jstl类库  
  --修改web.xml文件来配置ActionServlet  
  --提供struts-config.xml文件  
  --提供国际化资源文件  
 * 配置spring  
  --拷贝spring类库  
  --提供spring配置文件  
2、因为Action需要调用业务逻辑方法，所以需要在Action中提供setter方法，让spring将业务逻辑对象注入过来

3、在struts-config.xml文件中

配置Action  
  * &lt;action&gt;标签中的type属性需要修改为org.springframework.web.struts.DelegatingActionProxy  
   DelegatingActionProxy是一个Action，主要作用是取得BeanFactory，然后根据&lt;action&gt;中的path属性值  
   到IoC容器中取得本次请求对应的Action

如&lt;action path="/login"  
    type="org.springframework.web.struts.DelegatingActionProxy"  
    
4、在spring配置文件中

需要定义spring的Action,如：  
 &lt;bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype"&gt;  
  &lt;property name="userManager" ref="userManager"/&gt;  
 &lt;/bean&gt;  
 * 必须使用name属性，name属性值必须和struts-config.xml文件中&lt;action&gt;标签的path属性值一致  
 * 必须注入业务逻辑对象  
 * 建议将scope设置为prototype,这样就避免了struts Action的线程安全问题  
        
  二、部分程序

1、struts-config.xml

```
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.bjsxt.usermgr.forms.LoginActionForm"/>
	</form-beans>
	
	<action-mappings>
		
		<action path="/logininput"
				forward="/login.jsp"
		></action>
	
		<action path="/login"
				type="org.springframework.web.struts.DelegatingActionProxy"
				name="loginForm"
				scope="request"	
		>
			<forward name="success" path="/success.jsp"/>
		</action>
	</action-mappings>

    <message-resources parameter="MessageResources" />
</struts-config>
```

 

2、applicationcontext-action.xml

```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	<bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">
		<property name="userManager" ref="userManager"/>
	</bean>
</beans>
```

 

3、applicationcontext-bean.xml

```
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
	<bean id="userManager" class="com.bjsxt.usermgr.manager.UserManagerImpl"/>
</beans>
```

   
4、LoginAction .java

```
package com.bjsxt.usermgr.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.bjsxt.usermgr.forms.LoginActionForm;
import com.bjsxt.usermgr.manager.UserManager;

public class LoginAction extends Action {

	private UserManager userManager;
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//因为根据struts的配置文件<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"
//调用的时候会先生成代理，当生成代理后会去根据spring中name标识也是/login，通过IOC生成LoginAction的实例，同时也依赖注入（IOC）了userManager
		//所以下面就可以直接用userManager了。
		LoginActionForm laf = (LoginActionForm)form;
		
		userManager.login(laf.getUsername(), laf.getPassword());
		return mapping.findForward("success");
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
}
```