整合Struts与Spring的三种方式

本文详细介绍了如何在Struts框架中集成Spring框架,包括使用WebApplicationContext调用Spring Bean、配置Struts Action由Spring托管的方法及使用ActionSupport类进行集成。

1.在Struts 中使用WebApplicationContext 调用spring.
1)加载spring 的Bean文件。
<listener>
ContextLoaderListener
</listener>

2)访问spring 的Bean 对象。
WebapplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserBean userBean = (UserBean)ctx.getBean("userBean");

2.配置Struts 的Action 托管给Spring.
1) 在Struts-config.xml 中配置ContextLoaderPlugIn 插件,来加载spring 配置。
<set-property property ="" value="/WEB-INF/applicationContext.xml"/>

2)配置Action托管给spring.分两种方式:
(1)在struts-config.xml中配置使用spring的DelegatingRequestProcessor,
来重载struts默认的RequestProcessor.
使用DelegatingRequestProcessor

<controller>DelegatingRequestProcessor</controller>

<action path="/usr" type ="com.....UserAction"/>

<bean name="/usr" class ="com.....UserAction"/>

(2)将struts-config.xml中<action-mapping>的type 属性设为DelegatingActionProxy.
使用DelegatingActionProxy
<action path="/usr" type ="or.springframework.web.struts.DelegatingActionProxy">
<forward name="" path=""/>
</action>

<bean name ="" class="com.....UserAction"/>


3.继承Spring的ActionSupport 类。

public class UserAction extends DispatchActionSupport {
 public ActionForward execute(ActionMapping mapping,ActionForm form,
 HttpServletRequest request,HttpServletResponse response) throws Exception{

 WebApplicationContext ctx = getWebApplicationContext();
 UserBean userBean = (UserBean)ctx.getBean("userBean");
 return mapping.findForward("success");

  }


}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值