struts与spring集成方案

一、方案一(查找)

原理图:Action中取得BeanFactory,通过BeanFactory取得业务逻辑对象,IOC容器主动查找业务对象


实现步骤:

1springstruts的依赖包配置

*struts

--拷贝strutsjstl的依赖包

--web.xml文件中配置ActionServlet

--提供struts-config.xml文件

--提供国际化支持,提供缺省的国际化资源文件

*spring

--拷贝spring相关依赖包

--提供spring的配置文件

 

2、在web.xml文件中配置ContextLoaderListener,Web Server在启动的时候将

  BeanFactory放到ServletContext

  <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、在Action中采用WebApplicationContextUtils.getRequiredWebApplicationContext()ServletContext

  中取得BeanFactory

  

4、通过BeanFactoryIoC容器中取得业务逻辑对象

缺点:

           action看到spring相关东西,因为Action中出现了依赖查找,所以Action依赖SpringAPI,进一步了解依赖查找和依赖注入

二、方案二(注入)

原理图:StrutsAction交给Spring创建,这样业务逻辑对象将会被注入,这样就避免了依赖查找


实现步骤:

1springstruts的依赖包配置

*struts

--拷贝strutsjstl的依赖包

--web.xml文件中配置ActionServlet

--提供struts-config.xml文件

--提供国际化支持,提供缺省的国际化资源文件

*spring

--拷贝spring相关依赖包

--提供spring的配置文件

 

2、在web.xml文件中配置ContextLoaderListener,Web Server在启动的时候将

  BeanFactory放到ServletContext

  <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>

 

3struts-config.xml文件中<action>标签的type属性需要更改为Spring的代理Action类:org.springframework.web.struts.DelegatingActionProxy

  代理Action的作用:取得BeanFactory,然后到IoC容器中将本次请求对应的Action取出

  

4、将Action交给Spring创建,必须配置业务逻辑对象,注入给Action

<beanname="/login"class="com.bjpowernode.usermgr.web.actions.LoginAction">

<propertyname="userManager" ref="userManager"/>

</bean>

* 必须使用name属性,而且name属性的值必须和struts-config.xml文件中<action>标签的path属性值一致

* 必须配置业务逻辑对象

* 建议将scope设置为prototype,这样strutsAction将是线程安全的  



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值