struts+spring整合有3种方式,通过查阅资料得知,将servlet action 委托给spring来管理的这种方式优势最为显著.本次struts+spring整合测试就是采用委托的方法。
实施步聚:
首先在struts-config.xml文件中注册spring插件,如下所示。
代码
- <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" />
- </plug-in>
其次将所有action标签中type属性设为org.springframework.web.struts.DelegatingActionProxy 也就是将action委托给了spring
因而在spring上下文applicationContext.xml中配置一个于action标签path属性对应的bean(也就是bean的name值等于action的path值),如:
struts-config.xml代码片段
代码
- ......
- action-mappings>
- <action attribute="userForm" input="/user.jsp" name="userForm"
- path="/user" scope="request"
- type="org.springframework.web.struts.DelegatingActionProxy"
- validate="false" />
- </action-mappings>
- .....
applicationContext.xml代码片段
代码
- ......
- <bean name="/user" class="com.wiley.struts.action.UserAction" singleton="false" >
- <property name="manage">
- <ref bean="manage" />
- </property>
- </bean>
- ......
到此基本的配置工作完成了
在测试的过程中遇到servlet action无效,但控制台没有任何的提示,翻遍整个网络,e文看得是一知半解,中文资料很少,对我来说有价值的更少,将log4j增加上去后再次测试,原来是ContextLoaderPlugIn没有找到。在使用MyEclipse搭建spring环境时,请务必将spring-web也导入。
最后将spring-web加入后测试成功。