问题描述
如何在过滤器中检索FacesContext?
我关注了以下文章,介绍了如何在Filter
中检索FacesContext
:
http://ocpsoft.org/java/jsf-java/jsf-20-extension-development-accessing-facescontext-in-a-filter/
但是问题是它不适用于Flash范围.引发以下NPE:
java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.loggingGetPhaseMapForWriting(ELFlash.java:751)
at com.sun.faces.context.flash.ELFlash.getPhaseMapForWriting(ELFlash.java:785)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:392)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:112)
我想在过滤器中添加重定向,并使用Flash作用域保存一些数据以及Messages,这是行不通的.
您不能. FacesContext
由FacesServlet
创建,因此仅在FacesServlet
处理的任何Java代码中可用,该Java代码涵盖了所有JSF工件,例如托管Bean和阶段侦听器.本文仅显示了如何手动创建FacesContext
,但是这种方法最终没有用. FacesContext
只是标准Servlet API已提供的所有内容的抽象,例如HttpServletRequest
,HttpSession
,ServletContext
等.只需直接使用它们,就像JSF在幕后"做的一样. /p>
您有2个选择:
-
请改用JSF
PhaseListener
.根据具体的功能要求(您什么都没说),这可能是一个很笨拙的解决方案/解决方法. -
不要使用JSF提供的Flash作用域工具,而是自己自制.原理很简单:在初始请求上设置cookie,发送重定向,在重定向的请求中查找并将其删除(这样以后的请求中就不再存在该cookie).这正是JSF Flash范围在幕后的工作方式.另请参见将通知消息设置为一个具体示例,应该在sendRedirect 之后显示的request属性.
How do I retrieve the FacesContext within a Filter?
I followed following article on how to retrieve the FacesContext
in a Filter
:
http://ocpsoft.org/java/jsf-java/jsf-20-extension-development-accessing-facescontext-in-a-filter/
But the problem is that it is not working with Flash scope. Following NPE is thrown:
java.lang.NullPointerException
at com.sun.faces.context.flash.ELFlash.loggingGetPhaseMapForWriting(ELFlash.java:751)
at com.sun.faces.context.flash.ELFlash.getPhaseMapForWriting(ELFlash.java:785)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:392)
at com.sun.faces.context.flash.ELFlash.put(ELFlash.java:112)
I want to add redirection in my filter and use flash scope to save some data and also Messages, which is not working.
You cannot. The FacesContext
is created by the FacesServlet
and thus only available within any Java code which is processed by the FacesServlet
, which covers all JSF artifacts, such as managed beans and phase listeners. The article only shows how to manually create the FacesContext
, but this approach is ultimately useless. The FacesContext
is just an abstraction of everything already available by standard Servlet API such as HttpServletRequest
, HttpSession
, ServletContext
, etc. Just use them directly the same way as JSF is doing "under the hoods".
You have 2 options:
Use a JSF
PhaseListener
instead. Depending on the concrete functional requirement which you didn't tell anything about, this may be a rather clumsy solution/workaround.Don't use JSF-provided Flash scope facility, but homebrew one yourself. The principle is rather simple: set a cookie on initial request, send a redirect, in the redirected request lookup the cookie and remove it (so that it's not there anymore on any subsequent request). That's exactly how the JSF Flash scope works under the hoods. See also Set notification message as request attribute which should show after sendRedirect for a concrete example.
这篇关于如何在过滤器中检索FacesContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!