Java 在我的 Web 应用程序中从 spring 中获取“未找到线程绑定请求”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2039522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a 'No thread-bound request found' error from spring in my web app
提问by Chris
I'm getting a 'No thread-bound request found' error in my web app and hoping to get some help. I'm trying to use struts2 + spring + hibernate, and use spring to manage the hibernate session factory, and inject hibernate sessions into my struts actions. I hope that made sense. When the app starts up, there are no errors, but when i make the first web request it bombs out with the 'No thread-bound request found' error. Here's my spring config:
我的 Web 应用程序中出现“未找到线程绑定请求”错误,希望得到一些帮助。我正在尝试使用 struts2 + spring + hibernate,并使用 spring 来管理休眠会话工厂,并将休眠会话注入到我的 struts 操作中。我希望这是有道理的。当应用程序启动时,没有错误,但是当我发出第一个 Web 请求时,它会因“未找到线程绑定请求”错误而爆炸。这是我的弹簧配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="hibernateSessionFactory" scope="singleton"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml" />
</bean>
<bean id="hibernateSession" factory-bean="hibernateSessionFactory"
factory-method="openSession" destroy-method="close" scope="request" class="org.hibernate.Session" />
</beans>
Here's my action:
这是我的操作:
package actions.events;
import org.hibernate.Session;
public class Listing {
Session session;
public void setHibernateSession(Session value) throws Exception
{
session = value;
}
public String execute() {
return "success";
}
}
My only lead is that if i remove the 'setHibernateSession' function above, i don't get the error because presumably spring doesn't bother creating a session if the action doesn't need one (lazy instantiation).
我唯一的领导是,如果我删除上面的“setHibernateSession”函数,我不会收到错误消息,因为如果操作不需要会话(延迟实例化),spring 可能不会打扰创建会话。
And here's the exception:
这是例外:
Unable to instantiate Action, actions.events.Listing, defined for 'Listing' in namespace '/events'Error creating bean with name 'hibernateSession': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? 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, use RequestContextListener or RequestContextFilter to expose the current request.
com.opensymphony.xwork2.DefaultActionInvocation.createAction(DefaultActionInvocation.java:307)
com.opensymphony.xwork2.DefaultActionInvocation.init(DefaultActionInvocation.java:388)
com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:187)
org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:61)
org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:47)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:478)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
java.lang.Thread.run(Unknown Source)
Oh and the kicker is that my web.xml doeshave the necessary context listener, so the http request should be recognised by struts:
哦,关键是我的 web.xml确实有必要的上下文侦听器,所以 http 请求应该被 struts 识别:
<?xml version="1.0" encoding="UTF-8"?>
<web-app ...
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
...
</web-app>
采纳答案by axtavt
To use request scope without Spring MVC, you should declare RequestContextListener
in web.xml (see 3.5.4.1. Initial web configuration):
要在没有 Spring MVC 的情况下使用请求范围,您应该RequestContextListener
在 web.xml 中声明(请参阅3.5.4.1. 初始 Web 配置):
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>
回答by felipe
If you are configuring your app using Java instead of XML the same conf can be applied in
如果您使用 Java 而不是 XML 配置您的应用程序,则可以在
public void onStartup(ServletContext servletContext) throws ServletException {
//add listener
servletContext.addListener(new RequestContextListener());