---
title: "Spring与Struts集成_第一种方案摘录"
author: "whp0731"
date: "2009-03-30T13:38:40Z"
tags: ["AOP", "Spring", "Struts", "Web", "XML"]
slug: "spring-struts"
draft: false
lastmod: "2009-03-31T10:28:19Z"
id: 357564
url: "https://whp0731.iteye.com/blog/357564"
---

一、原理：

在Action中取得BeanFactory对象，然后通过BeanFactory获取业务逻辑对象

![](/upload/attachment/89019/e8631bf7-907f-3154-a00a-392cca00c557.jpg)  
 

1、spring和struts依赖库配置  
 * 配置struts  
  --拷贝struts类库和jstl类库  
  --修改web.xml文件来配置ActionServlet  
  --提供struts-config.xml文件  
  --提供国际化资源文件MessageResources.properties  
 * 配置spring  
  --拷贝spring类库  
  --提供spring配置文件  
    
2、在struts的Action中调用如下代码取得BeanFactory  
 BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

//这个方法的前提是在web.xml中加入了如下的配置  
  /\*&lt;context-param&gt;  
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
    &lt;param-value&gt;classpath\*:applicationContext-\*.xml&lt;/param-value&gt;  
     &lt;/context-param&gt;  
     
  &lt;listener&gt;  
   &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
  &lt;/listener&gt;\*/  
  //这个配置的作用是当spring启动时，最先调用listener，那么就会在容器中加入BeanFactory实例对象。以后需要使用时通过以下语句去拿就可以了。这样就不会重复生成BeanFactory 对象了  
  

 

3、通过BeanFactory取得业务对象，调用业务逻辑方法   

 

二、具体实例程序

1、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>
```

   
  2、web.xml加入部分

```
<context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath*:applicationContext-*.xml</param-value>
  </context-param>
   
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
```

 

3、loginAction

```
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 org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

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

public class LoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginActionForm laf = (LoginActionForm)form;
		//这里第一种userManager 的生成方式是用new的方式，没有用依赖注入
		//UserManager userManager = new UserManagerImpl();
		//userManager.login(laf.getUsername(), laf.getPassword());
		
		//这里第二种userManager的生成方式是用的spring的工厂的方式，虽然用了依赖注入，但是这种方式也有他的缺陷
		//打个比方如果需要在多个类中应用，那么需要生成多个BeanFactory的对象，所以有了下面的方法
		//BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-beans.xml");
		//UserManager userManager = (UserManager)factory.getBean("userManager");
		//userManager.login(laf.getUsername(), laf.getPassword());
		
		
		//这个方法的前提是在web.xml中加入了如下的配置
		/*<context-param>
	  	<param-name>contextConfigLocation</param-name>
	  	<param-value>classpath*:applicationContext-*.xml</param-value>
	    </context-param>
	   
		<listener>
			<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>*/
		//这个配置的作用是当spring启动时，最先调用listener，那么就会在容器中加入BeanFactory实例对象。以后需要使用时通过以下语句去拿就可以了
		BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		
		//以下这句其实和上面那一句是一致的，只不过它让返回值更加明确了，ApplicationContext其实就是BeanFactory的一个子接口（功能更多）
		//ApplicationContext pc = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
		UserManager userManager = (UserManager)factory.getBean("userManager");
		userManager.login(laf.getUsername(), laf.getPassword());
		
		return mapping.findForward("success");
	}

	
}
```

 

 

4、struts配置

从以下配置文件的 &lt;action path="/login"  
type="com.bjsxt.usermgr.actions.LoginAction"  
name="loginForm"  
scope="request" &gt;  
&lt;forward name="success" path="/success.jsp"/&gt;  
&lt;/action&gt;  
可以看出，在这个方案中与下一个方案的区别，action还是通过struts实例化取得的，不是通过spring的IOC来创建action的，在这个方案中只是UserManagerImpl业务实现实现了IOC

```
<?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="com.bjsxt.usermgr.actions.LoginAction"
				name="loginForm"
				scope="request"	
		>
			<forward name="success" path="/success.jsp"/>
		</action>
	</action-mappings>

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

```

 

5、整体见附件