左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实现IOC的功能需要做如下几步工作:
(1)、
web.xml中添加如下配置:
- <listener>
- <listener-class>
- org.springframework.web.context.ContextLoaderListener
- </listener-class>
- </listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
(2)、需要修改
struts.properties配置文件:
- struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
struts.objectFactory = org.apache.struts2.spring.StrutsSpringObjectFactory
注意,很多网上的文章写的是:- struts.objectFactory = spring
struts.objectFactory =spring
我个人配置的经验是,这样配置可能会报错,原因我也不知道……。(3)、引入
spring.jar和struts2-
spring-plugin-2.0.11.1.jar两个jar包。
(4)、将
struts的*Action.java以bean的形式写在
spring的配置文件中,如下代码所示:
- <bean id="userAction" class="com.demo.web.user.UserAction" />
<bean id="userAction" class="com.demo.web.user.UserAction" />
(5)、将
spring的配置文件加入web.xml中,形如:
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:spring/applicationContext*.xml
- </param-value>
- </context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring/applicationContext*.xml
</param-value>
</context-param>
(6)、修改strtus.xml配置文件:
将原来的
- ……
- <action name="user" class="com.demo.web.user.UserAction">
- <result name="reload" type="redirect-action">user</result>
- </action>
- ……
……
<action name="user" class="com.demo.web.user.UserAction">
<result name="reload" type="redirect-action">user</result>
</action>
……
改为:
- ……
- <action name="user" class="userAction">
- <result name="reload" type="redirect-action">user</result>
- </action>
- ……
……
<action name="user" class="userAction">
<result name="reload" type="redirect-action">user</result>
</action>
……
完成这个过程后,基本上算大功告成。
但是,在实际的配置过程中,需要注意如下问题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方法实现的。
不管怎么样,解决办法是有的,其实该错误本身就说了解决办法(use
RequestContextListeneror RequestContextFilter to expose the current request):
在web.xml中添加:
- <listener>
- <listener-class>
- org.springframework.web.context.request.RequestContextListener
- </listener-class>
- </listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
重新启动,你会发现,现在错误没有了。
2、关于
struts.properties的两个参数设置:
- #该参数设置spring管理的struts bean的装配模式
- #有如下参数可供选择
- #name 按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的
- #type 按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean
- #auto Spring会试图自动监测来找到最好的方法自动装配你的action
- #constructor Spring会自动装配bean的构造函数的参数
-
- struts.objectFactory.spring.autoWire = type
- #是否使用类缓存。你可以通过设置修改struts.properties中下列属性的值来改变是否使用Spring自身的类缓存机制。可以设定的值为true或false,默认为true
- struts.objectFactory.spring.useClassCache = false
导入论坛
引用链接
收藏
分享给好友
推荐到圈子
管理
举报
TAG: