问题描述
为什么从不同于的上下文中调用 RequestContext rq = RequestContext.getCurrentInstance()
会引发 NullPointerException
吗?
Why calling RequestContext rq = RequestContext.getCurrentInstance()
from a different context than FacesContext
throws a NullPointerException
?
我无法执行以下操作:
RequestContext rq = RequestContext.getCurrentInstance() //NPE is thrown
if (rq != null) {
..
}
我想做的是在 WebFilter
中检索一个bean并调用一个方法.此方法使用上面的代码段;因此会抛出 NullPointerException
.
What I am trying to do is to retrieve a bean inside a WebFilter
and call a method. This method uses above snippet; so it throws NullPointerException
.
谢谢您的帮助.
推荐答案
RequestContext
的实例另存为 FacesContext
中的属性,因此当没有时FacesContext
,您将拥有NPE.这是告诉您如何获取 RequestContext
的代码,该代码将被清除:
Instance of RequestContext
is saved as an attribute in FacesContext
, so when there is not FacesContext
you will have NPE. Here is the code which will tell you how RequestContext
is obtained, and that will be cleared:
return (RequestContext) FacesContext.getCurrentInstance().getAttributes().get(Constants.REQUEST_CONTEXT_ATTR);
创建 RequestContext
并将其另存为 FacesContext
属性是在 还原视图阶段 ,因此它在您的过滤器中不存在.
Creating RequestContext
and saving it as FacesContext
attribute is done in PhaseListener
of Primefaces, after Restore view phase, so it doesn't exist in your filter.
这篇关于RequestContext从与FacesContext不同的上下文中引发NPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!