Search code examples
jsfjsf-2primefacesmanaged-bean

java.lang.NullPointerException at org.primefaces.context.RequestContext.getCurrentInstance


I used a RequestContext's primeFaces object in a managed bean method in order to execute some javascript from the server side. I used to do this inside a Listener handler method (a method with an AjaxBehaviourEvent parameter handling requests from f:ajax tags) and it works but this time i used a method without any parameters.

The problem is that i m appending some html with jquery .append() and had to use jquery ajax to send requests to server from added elements (i can't append any jsf components because they won't work as they are not generated by jsf itself) so I had to use bean methods without parameters and call that method from a servlet which handle ajax comming resquests.

to explain this more in details, suppose i add html code with jquery this way :

$('#substartmenudiv').append('<div id="congesdiv" />');

and then add the code which must be executed once the new added div is clicked :

$('#congesdiv').click(function(){
 $.post('AjaxRelaisServlet',{action:"setstrtmenustatus",startmenuisopen:startmenuisopen});
});

and from the servlet i catch parameters sent by the ajax request and call the bean method :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


boolean startmenuisopen= Boolean.valueOf(request.getParameter("startmenuisopen"));

        UserBean.testcmdlink(startmenuisopen);

the called testcmdlink bean method contains the RequestContext object, it looks like this :

public static void testcmdlink(boolean startmenuisopen){        

        RequestContext context = RequestContext.getCurrentInstance();
        if(!startmenuisopen){
        context.execute("$('#substartmenudiv').append('<div id="+"gestprofdiv"+" />');");
        context.execute("displaymenuitems();");
        context.execute("console.log('more elements are appended !!');");
        startmenuisopen=true;

        }else{
            context.execute("$('#substartmenudiv').empty();");
            context.execute("window['bottomvalue'] =30;");
            context.execute("console.log('start menu div is emptied !!');");
            startmenuisopen=false;
        }

    }

Is there a way to make it work anywhere ?

cheers !


Solution

  • You can't do that. Under the hood, RequestContext relies on the FacesContext Object. This object is not available outside of a JSF lifecycle processing.

    You might be able to cheat this by finding a way to stuff get the Constants.REQUEST_CONTEXT_ATTR object into the general ServletContext during a proper JSF request and then retrieve the object within the servlet. I'm going on this leap of faith based on the implementation of RequestContext.getCurrentInstance() in the PF source

    public static RequestContext getCurrentInstance(){
     return (RequestContext)FacesContext.getCurrentInstance().getAttributes().get(Constants.REQUEST_CONTEXT_ATTR);
    }