解決問題(七)——jsf+spring+hibernate整合(一)

jsf+spring+hibernate整合,於struts+spring+hibernate整合的過程非常相似(就不截圖了,可以參考第一篇文章),但是前者相對於後者稍微麻煩一些。

第一步:初步整合,其過程以及基本配置,於struts+spring+hibernate整合的過程完全一樣。

第二步:要加入jsf-spring.jar(可以到SourceForge上面去下)

第三步:在web.xml中加入如下listener

<listener>
  <listener-class>
   org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>
 <listener>
  <listener-class>
   de.mindmatters.faces.spring.context.ContextLoaderListener
  </listener-class>
 </listener>

二者都要加,而且順序不能變。

最好在加入下面的listener順序沒影響

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

否則會遇到下面的錯誤

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: /admin/login.jsp(16,4) '#{PersonBean.loginName}' de.mindmatters.faces.spring.factory.BeansEvaluationException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PersonBean': Scope 'session' 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? 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.
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:541)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	com.jsha.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:57)

 

root cause

javax.servlet.ServletException: /admin/login.jsp(16,4) '#{PersonBean.loginName}' de.mindmatters.faces.spring.factory.BeansEvaluationException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'PersonBean': Scope 'session' 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? 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.
	javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
	org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:694)
	org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:665)
	org.apache.jsp.index_jsp._jspService(index_jsp.java:56)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	com.jsha.filter.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:57)
控制檯上的錯誤不太一樣
java.lang.IllegalStateException: 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, use RequestContextListener or RequestContextFilter to expose the current request.
 at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:102)
 at org.springframework.web.context.request.SessionScope.get(SessionScope.java:88)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:283)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:160)
 at de.mindmatters.faces.spring.factory.DelegatingVariableResolver.resolveVariable(DelegatingVariableResolver.java:101)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章