在我的web应用程序中从spring获取'找不到线程绑定的请求'错误 [英] Getting a 'No thread-bound request found' error from spring in my web app

查看:270
本文介绍了在我的web应用程序中从spring获取'找不到线程绑定的请求'错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的web应用程序中收到'没有线程绑定的请求'错误,希望得到一些帮助。
我尝试使用struts2 + spring + hibernate,并使用spring来管理hibernate会话工厂,并将hibernate会话注入到struts动作中。我希望这是有道理的。
当应用程序启动时,没有错误,但是当我发出第一个Web请求时,它会弹出'找不到线程绑定请求'错误。
这里是我的spring配置:

 <?xml version =1.0encoding =UTF-8? > 
<!DOCTYPE beans PUBLIC - // SPRING // DTD BEAN 2.0 // ENhttp://www.springframework.org/dtd/spring-beans-2.0.dtd\">
< beans>
< bean id =hibernateSessionFactoryscope =singleton
class =org.springframework.orm.hibernate3.LocalSessionFactoryBean>
< property name =configLocationvalue =classpath:hibernate.cfg.xml/>
< / bean>
< bean id =hibernateSessionfactory-bean =hibernateSessionFactory
factory-method =openSessiondestroy-method =closescope =requestclass =org.hibernate.Session />
< / beans>

这是我的动作:

  package actions.events; 
import org.hibernate.Session;

public class Listing {
Session session;
public void setHibernateSession(Session value)throws Exception
{
session = value;
}

public String execute(){
returnsuccess;






$ b我只有如果我删除'setHibernateSession '函数上面,我没有得到错误,因为大概春天不会创建一个会话,如果行动不需要一个(懒惰实例化)。



以及这里是例外:

 无法实例化在命名空间'/ events'中为'Listing'定义的Action,actions.events.Listing创建名为'hibernateSession'的Bean时出错:范围'请求'对当前线程无效;考虑为这个bean定义一个范围代理,如果你打算从一个单例中引用它;嵌套异常是java.lang.IllegalStateException:找不到线程绑定的请求:您是否在实际Web请求之外引用请求属性,或者在原始接收线程之外处理请求?如果实际上在Web请求中运行并仍然收到此消息,则代码可能在DispatcherServlet / DispatcherPortlet之外运行:在这种情况下,请使用RequestContextListener或RequestContextFilter来公开当前请求。 
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.catalin a.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:
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.Http11Proc essor.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)

哦,而且kicker是我的web.xml 具有必要的上下文监听器,所以http请求应该被struts识别:

 <?xml version =1.0encoding =UTF-8?> 
< web-app ...
...
< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>
...
< / web-app>


解决方案

要使用没有Spring MVC的请求范围,您应该声明web.xml中的 RequestContextListener (参见 3.5.4.1。初始网络配置):

 < web应用程序> 
...
< listener>
< listener-class>
org.springframework.web.context.request.RequestContextListener
< / listener-class>
< / listener>
...
< / web-app>


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:

<?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).

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 does have the necessary context listener, so the http request should be recognised by struts:

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...
  ...
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  ...
</web-app>

解决方案

To use request scope without Spring MVC, you should declare RequestContextListener in web.xml (see 3.5.4.1. Initial web configuration):

<web-app>
  ...
  <listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
  ...
</web-app>

这篇关于在我的web应用程序中从spring获取'找不到线程绑定的请求'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆