DelegatingActionProxy

[align=center][size=xx-large][b][color=red]DelegatingActionProxy[/color][/b][/size][/align]

org.springframework.web.struts.DelegatingActionProxy
在目录spring-framework-2.5.4\dist\modules下的spring-webmvc-struts.jar

如何将Struts1.x和spring整合在一起,让spring去管理struts中的Action实例,action里面属性的注入?

必看:[url]http://www.iteye.com/topic/18348[/url]
[url]http://java.chinaitlab.com/Struts/767495_3.html[/url]

1.在web.xml加入:
	<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:spring/core/applicationContext.xml
</param-value>
</context-param>


2.修改struts-config.xml中action节点的配置信息,将原来定义的
<action path="/validateUser"
type="com.wuwei.struts.action.ValidateUserAction" name="userForm">
<forward name="success" path="/success.jsp"></forward>
<forward name="fail" path="/fail.jsp"></forward>
</action>
中的type改为: type="org.springframework.web.struts.DelegatingActionProxy">或者你也可以不改,而在<plug-in>标签前加入如下定义

<!-- 加上controller就不用再配置action的type属性了/或者说type属性不用改为
type="org.springframework.web.struts.DelegatingActionProxy" -->
<controller
processorClass="org.springframework.web.struts.DelegatingRequestProcessor">
</controller>

3.在applicationContext.xml中为ValidateUserAction添加定义,将userDao的实例化加给Spring,这也是Spring中的依赖注入
<!-- Struts -->
<bean name="/validateUser"
class="com.wuwei.struts.action.ValidateUserAction">
<property name="userDAO" ref="userDao"/>
</bean>

当然,这里ref(引用)的Bean userDao也是在applicationContext.xml中己定义好的

<!-- 通过HibernateDaoSupport来操作数据库,需要植入sessionFactory
UserDao继承自HibernateDaoSupport
-->
<bean id="userDao" class="com.wuwei.struts.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory" />
</bean>


[size=medium][b][color=green]二、使用 DelegatingActionProxy[/color][/b][/size]


使用 DelegatingRequestProcesso 非常简单方便,但有一个缺点:RequestProcessor 是Struts 的一个扩展点,也许应用程序本身就需要扩展RequestProcessor ,而DelegatingRequest Processor 已经使用了这个扩展点。

为了重新利用 Struts 的 RequestProcessor 这个扩展点,有以下两个方法:使应用程序的 RequestProcessor 不再继承 Struts 的 RequestProcessor ,改为继承DelegatingRequestProcessor 。

使用 DelegatingActionProxy。

前者常常有一些未知的风险,而后者是 Spring 推荐的整合策略。使用 DelegatingActionProxy 与DelegatingRequestProcessor 的目的只有一个,都是将请求转发给 Spring管理的 bean。

DelegatingRequestProcessor 可直接替换了原有的 RequestProcessor,并在请求转发给action 之前,转发给 Spring 管理的 bean; 而 DelegatingActionProxy 则被配置成 Struts 的action,即所有的请求先被 ActionServlet拦截,然后将请求转发到对应的 action,而 action的实现类全都是 DelegatingActionProxy; 最后由 DelegatingActionProxy 将请求转发给Spring 容器的 bean(真正的处理类action)。

这个bean可以配置在applicationContext.xml中,也可以将它从里面抽取出来,配置在专门的applicationContext-action.xml中,并在applicationContext.xml里面配置该文件名的映射路径。

<bean name="/datadictAdmin" class="com.lenovo.platform.admin.web.action.DatadictAdminAction">
<property name="datadictService" ref="datadictService" />
</bean>


可以看出:使用 DelegatingActionProxy 比使用 DelegatingRequestProcessor 要晚一步转发到 Spring 的 contexto 但通过这种方式可以避免占用扩展点。与使用 DelegatingRequestProcessor 对比,使用 DelegatingActionProxy 仅需要去掉controller 配置元素,并将所有的 action 实现类改为 DelegatingActionProxy 即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值