问题
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