2

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 !

kolossus
  • 20,559
  • 3
  • 52
  • 104
Bardelman
  • 2,176
  • 7
  • 43
  • 70
  • Please post code which explains what you want to do in that AJAX request, and method you want to call. – partlov Mar 29 '13 at 11:51
  • Hi BalusC !! i need u for my previous question please check http://stackoverflow.com/questions/15691240/servlet-3-0-doesnt-work – Bardelman Mar 29 '13 at 12:44
  • i know some others ways can be made to get the same result but i need more control of the view from the server-side becose i will need to save html components css properties in the data base. Actually i need this way to work becose i ll need it more for the future. – Bardelman Mar 29 '13 at 12:51
  • This is a tad confusing. Are you accessing `RequestContext` in a servlet or managed bean? – kolossus Mar 29 '13 at 13:15
  • as u can see the testcmdlink is accessed by a static way from the servlet, i forgot to write the "static" in the method when i copied the method in here, sorry. – Bardelman Mar 29 '13 at 13:49

2 Answers2

2

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);
} 
Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • it seems there still a hope to use a RequestContext outside the processing lifecycle as in the BalusC's article that u mentioned in your previous post but i still need to know how to get RequestContext from the FacesContext and i wonder this is possible as the RequestContext i actually need is a org.primefaces.context.RequestContext which have a method execute(..JS..) – Bardelman Mar 29 '13 at 17:17
  • Instance of RequestContext is saved as an attribute in FacesContext.i found an answer to my question here : http://stackoverflow.com/questions/15428342/requestcontext-throws-npe-from-a-different-context-than-facescontext – Bardelman Mar 29 '13 at 18:19
  • @user1705922, yup, looks like it. So combining BalusC's hack and the suggestion, you'll get what you want. Congrats. I'm updating – kolossus Mar 29 '13 at 18:27
  • sorry , i didn't see that u have edited your answer and told me that :D – Bardelman Mar 29 '13 at 18:31
  • After some tests, i have to say that working with FacesContext outside the processing lifecycle have no sense as the FacesContext instance is request scoped (means its content is volatile) as mentioned here http://stackoverflow.com/questions/4605118/jsf2-exeptions-while-submiting-to-bean . So, even i got the FacesContext instance, i found its attributes map almost empty (i just found {com.sun.faces.INVOCATION_PATH=/AjaxRelaisServlet}) and even i tried to put a personalized RequestContext on its parameters map it doesn't work. – Bardelman Mar 30 '13 at 10:13
  • As a conclusion, i think that the appropriate way to achieve what i want is to manipulate JSF graphical components programmatically with JSF itself not with jquery. i have to review JSF references to know how it can be done. – Bardelman Mar 30 '13 at 10:54
0

You can find the answer in your question. I assume you have a Listener handler method which listens for request and hanldes them with parameters. So you need to pass some parameters or you need to create a new method which accepts no parameter to process your requests.. When there is no parameters it throws a NullPointerException.

Hope it helps!!!

Lucky
  • 16,787
  • 19
  • 117
  • 151