RequestContext throws NPE from a different context than FacesContext

两盒软妹~` 提交于 2020-01-17 02:39:29

问题


Why calling RequestContext rq = RequestContext.getCurrentInstance() from a different context than FacesContext throws a NullPointerException ?

I am not able to do something like :

RequestContext rq = RequestContext.getCurrentInstance() //NPE is thrown
if (rq != null) {
..
}

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.

Thank you for your help.


回答1:


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);

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.



来源:https://stackoverflow.com/questions/15428342/requestcontext-throws-npe-from-a-different-context-than-facescontext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!