您的位置:首页 > 产品设计 > UI/UE

解决Scope 'request' is not active for the current thread异常

2014-03-13 21:47 881 查看
在spring整合struts中,spring配置文件中的action bean一定要配置scope属性,因为每次请求的信息包含在action中

<bean id="loginAction" class="com.wzy.app.action.LoginAction" scope="request">
<property name="myService" ref="myService" />
</bean>

但是这会抛出一个异常:Scope 'request' is not active for the current thread
百度一下,大概的解释是:

要使用request、session和 global session作用域的bean(即具有web作用域的bean), 在开始设置bean定义之前,还要做少量的初始配置。请注意,假如你只想要“常规的”作用域,(singleton和prototype),就不需要这一额外的设置。 
在目前的情况下,根据你的特定servlet环境,有多种方法来完成这一初始设置...
如果你用Spring Web MVC,即用SpringDispatcherServlet或DispatcherPortlet来处理请求,则不需要做特别的配置:DispatcherServlet 和 DispatcherPortlet已经处理了所有有关的状态
当使用了Spring's DispatcherServlet以外的Servlet 2.4及以上的Web容器时(如使用JSF或Struts),你需要在Web应用的'web.xml'文件中增加 javax.servlet.ServletRequestListener 定义

在web.xml中添加定义

<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>针对servlet2.4以及之前版本的情况,使用Filter配置如下:
<filter>
<filter-name>requestContextFilter</filter-name>
<filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
RequestContextListener和RequestContextFilter两个类做的都是同样的工作: 将HTTP request对象绑定到为该请求提供服务的Thread。 这使得具有request和session作用域的bean能够在后面的调用链中被访问到。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐