:arrow: 断断续续的学习spring又一个月了,领略到了一些其中的要点。几天成功的把spring2.0+hibernate3.0+struts1.2整合到了一起。尤其是在最后整合spring+struts遇到了问题,从网络中找到了整合方法,但太啰嗦,而且各种没有必要的代码也混在了一起。所以我自己写一个spring2+struts1.2整合的方式。
struts-config.xml中<action>标签中的元素需要重新配置
[color=red]尤其需要注意:type的变化![/color]
struts-config.xml中的加入<plug-in>元素
applicationContext.xml中利用代理生成action对象
其中accountService是自己的服务逻辑,当然,需要在对应的action中配置好对应的accountService变量,以及该变量的setter方法。关键代码如下:
其他方面的struts使用习惯,完全可以保持不变,现在我可以理解为什么说spring的主要作用是解藕,还有那种组件。如果有必要,完全可以把struts的表示层换成其他的框架。而且持久层也很方便的就可以更换,很类似一堆可以任意组合的插件。
当然,以上列出的方法只是整合方法之一,也是我自己动手实践了的~~
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的表示层换成其他的框架。而且持久层也很方便的就可以更换,很类似一堆可以任意组合的插件。
当然,以上列出的方法只是整合方法之一,也是我自己动手实践了的~~