struts 与spring整合关键点

本文介绍了一种将Spring 2.0与Struts 1.2整合的方法,并详细解释了如何配置struts-config.xml文件及applicationContext.xml文件来实现两者的有效结合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

:arrow: 断断续续的学习spring又一个月了,领略到了一些其中的要点。几天成功的把spring2.0+hibernate3.0+struts1.2整合到了一起。尤其是在最后整合spring+struts遇到了问题,从网络中找到了整合方法,但太啰嗦,而且各种没有必要的代码也混在了一起。所以我自己写一个spring2+struts1.2整合的方式。

struts-config.xml中<action>标签中的元素需要重新配置
[color=red]尤其需要注意:type的变化![/color]

<action attribute="transferForm" input="jsps/Input.jsp"
name="transferForm" path="/transfer" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="failed" path="/jsps/TransferFailed.jsp" />
<forward name="success" path="/jsps/TransferSuccess.jsp" />
</action>


struts-config.xml中的加入<plug-in>元素

<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>


applicationContext.xml中利用代理生成action对象

<bean name="/transfer" class="com.cxz.bank.action.TransferAction">
<property name="accountService" ref="accountService"/>
</bean>

其中accountService是自己的服务逻辑,当然,需要在对应的action中配置好对应的accountService变量,以及该变量的setter方法。关键代码如下:

public class TransferAction extends Action {

private AccountService accountService = null;

public void setAccountService(AccountService accountService) {
this.accountService = accountService;
}

public ActionForward execute(ActionMapping mapping, ActionForm form,
//使用改服务类,进行业务逻辑的操作。
return null;
}
}

其他方面的struts使用习惯,完全可以保持不变,现在我可以理解为什么说spring的主要作用是解藕,还有那种组件。如果有必要,完全可以把struts的表示层换成其他的框架。而且持久层也很方便的就可以更换,很类似一堆可以任意组合的插件。

当然,以上列出的方法只是整合方法之一,也是我自己动手实践了的~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值