左java,右oracle;肚大发少终不悔! 金钱不是创业的唯一资本!

struts与spring整合,以控制action周期

上一篇 / 下一篇  2008-11-05 11:56:05 / 个人分类:struts+spring+hibernate

转:http://javeye.javaeye.com/blog/238559
有时间将xml文件copy过来

struts2使用spring实现IOC时的配置和要注意到问题

在struts2中,通过spring来实现其IOC有很多的文章都有介绍,本文将总结一下其配置过程。此外,对几个很多文章都没涉及到容易出错的地方,在这里也一并总结一下,希望能给大家在配置的过程中提供帮助,少走弯路。
要想让struts2实现IOC的功能需要做如下几步工作:
(1)、web.xml中添加如下配置:
Xml代码 
  1. <listener>  
  2.     <listener-class>  
  3.         org.springframework.web.context.ContextLoaderListener  
  4.     </listener-class>  
  5. </listener>  

(2)、需要修改struts.properties配置文件:
Xml代码 
  1. struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory  

注意,很多网上的文章写的是:
Xml代码 
  1. struts.objectFactory = spring  

我个人配置的经验是,这样配置可能会报错,原因我也不知道……。
(3)、引入spring.jar和struts2-spring-plugin-2.0.11.1.jar两个jar包。
(4)、将struts的*Action.java以bean的形式写在spring的配置文件中,如下代码所示:
Xml代码 
  1. <bean id="userAction" class="com.demo.web.user.UserAction" />  

(5)、将spring的配置文件加入web.xml中,形如:
Xml代码 
  1. <context-param>  
  2.     <param-name>contextConfigLocation</param-name>  
  3.     <param-value>  
  4.         classpath:spring/applicationContext*.xml  
  5.     </param-value>  
  6. </context-param>  

(6)、修改strtus.xml配置文件:
将原来的
Xml代码 
  1. ……  
  2.         <action name="user" class="com.demo.web.user.UserAction">  
  3.             <result name="reload" type="redirect-action">user</result>  
  4.         </action>  
  5. ……  

改为:
Xml代码 
  1. ……  
  2.         <action name="user" class="userAction">  
  3.             <result name="reload" type="redirect-action">user</result>  
  4.         </action>  
  5. ……  


完成这个过程后,基本上算大功告成。
但是,在实际的配置过程中,需要注意如下问题
1、关于调用struts的action时出现如下错误:
No thread-bound request found: Are you referring to request attributes outside of an actual web request? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, useRequestContextListeneror RequestContextFilter to expose the current request.
必然,这个错误是由于IOC引起的,如果不用IOC的方式,这个错误就不会出现。一种出现该错误的情况在于你的Action配置了 session,request或则response。大家都知道在struts2中,request和response的实现机制已经大不一样了,不以 参数的形式存在于方式之中,而是通过实现SessionAware, ServletRequestAware,ServletResponseAware接口的set方法实现的
不管怎么样,解决办法是有的,其实该错误本身就说了解决办法(useRequestContextListeneror RequestContextFilter to expose the current request):
在web.xml中添加:
Xml代码 
  1. <listener>  
  2.     <listener-class>  
  3.         org.springframework.web.context.request.RequestContextListener  
  4.     </listener-class>  
  5. </listener>  

重新启动,你会发现,现在错误没有了。
2、关于struts.properties的两个参数设置:
Xml代码 
  1. #该参数设置spring管理struts bean的装配模式  
  2. #有如下参数可供选择  
  3. #name 按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的   
  4. #type 按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean   
  5. #auto Spring会试图自动监测来找到最好的方法自动装配你的action   
  6. #constructor Spring会自动装配bean的构造函数的参数   
  7.   
  8. struts.objectFactory.spring.autoWire = type  
  9. #是否使用类缓存。你可以通过设置修改struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true  
  10. struts.objectFactory.spring.useClassCache = false 


TAG:

引用 删除 Guest   /   2012-02-06 13:57:50
5
引用 删除 Guest   /   2012-02-06 09:31:44
3
引用 删除 Guest   /   2011-09-16 09:30:37
-5
引用 删除 Guest   /   2011-07-26 14:38:48
5
引用 删除 Guest   /   2011-03-07 14:31:03
5
引用 删除 Guest   /   2010-10-27 10:42:39
-5
引用 删除 Guest   /   2010-01-06 11:10:35
-5
引用 删除 Guest   /   2009-08-14 15:04:58
1
 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

Open Toolbar