现在的位置: 首页 > 综合 > 正文

struts+spring整合测试

2018年01月16日 ⁄ 综合 ⁄ 共 1219字 ⁄ 字号 评论关闭

struts+spring整合有3种方式,通过查阅资料得知,将servlet action 委托给spring来管理的这种方式优势最为显著.本次struts+spring整合测试就是采用委托的方法。
实施步聚:
首先在struts-config.xml文件中注册spring插件,如下所示。

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


其次将所有action标签中type属性设为org.springframework.web.struts.DelegatingActionProxy 也就是将action委托给了spring
因而在spring上下文applicationContext.xml中配置一个于action标签path属性对应的bean(也就是bean的name值等于action的path值),如:
struts-config.xml代码片段

代码
  1. ......   
  2. action-mappings>  
  3.  <action attribute="userForm" input="/user.jsp" name="userForm"  
  4.   path="/user" scope="request"  
  5.   type="org.springframework.web.struts.DelegatingActionProxy"  
  6.   validate="false" />  
  7. </action-mappings>  
  8. .....  


applicationContext.xml代码片段

代码
  1. ......   
  2.  <bean name="/user" class="com.wiley.struts.action.UserAction" singleton="false" >  
  3.   <property name="manage">  
  4.    <ref bean="manage" />  
  5.   </property>  
  6.  </bean>  
  7. ......  


到此基本的配置工作完成了
在测试的过程中遇到servlet action无效,但控制台没有任何的提示,翻遍整个网络,e文看得是一知半解,中文资料很少,对我来说有价值的更少,将log4j增加上去后再次测试,原来是ContextLoaderPlugIn没有找到。在使用MyEclipse搭建spring环境时,请务必将spring-web也导入。
最后将spring-web加入后测试成功。 

抱歉!评论已关闭.