在我的网络应用程序中从spring获取"未找到线程绑定请求"错误

Chr*_*ris 25 java spring struts hibernate struts2

我在我的网络应用程序中收到"未找到线程绑定请求"错误,并希望得到一些帮助.我正在尝试使用struts2 + spring + hibernate,并使用spring来管理hibernate会话工厂,并将hibernate会话注入我的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>
Run Code Online (Sandbox Code Playgroud)

这是我的行动:

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";
  }
}
Run Code Online (Sandbox Code Playgroud)

我唯一的领先是,如果我删除上面的'setHibernateSession'函数,我不会得到错误,因为如果动作不需要一个(懒惰的实例化),大概春天不打扰创建会话.

这是例外:

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)
Run Code Online (Sandbox Code Playgroud)

哦,踢球者是我的web.xml 确实有必要的上下文监听器,所以struts应该识别http请求:

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...
  ...
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  ...
</web-app>
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 51

要在没有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>
Run Code Online (Sandbox Code Playgroud)

  • 坚持下去,如果我包括*两个*听众,它的确有效!RequestContextListener和ContextLoaderListener一起工作.我同时使用两者都不好吗? (3认同)
  • 非常感谢!你比官方的春季论坛更有帮助.这完全解释了,好的. (3认同)
  • 它们用于不同的目的 - ContextLoaderListener加载Spring应用程序上下文,然后RequestContextListener为请求范围的bean提供支持 (2认同)

fel*_*ipe 5

如果您使用Java而不是XML配置应用程序,则可以应用相同的conf

 public void onStartup(ServletContext servletContext) throws ServletException {
          //add listener
          servletContext.addListener(new RequestContextListener());
Run Code Online (Sandbox Code Playgroud)