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 !

5
  • Please post code which explains what you want to do in that AJAX request, and method you want to call. Commented Mar 29, 2013 at 11:51
  • Hi BalusC !! i need u for my previous question please check stackoverflow.com/questions/15691240/servlet-3-0-doesnt-work Commented Mar 29, 2013 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. Commented Mar 29, 2013 at 12:51
  • This is a tad confusing. Are you accessing RequestContext in a servlet or managed bean? Commented Mar 29, 2013 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. Commented Mar 29, 2013 at 13:49

2 Answers 2

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);
} 
Sign up to request clarification or add additional context in comments.

6 Comments

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..)
Instance of RequestContext is saved as an attribute in FacesContext.i found an answer to my question here : stackoverflow.com/questions/15428342/…
@user1705922, yup, looks like it. So combining BalusC's hack and the suggestion, you'll get what you want. Congrats. I'm updating
sorry , i didn't see that u have edited your answer and told me that :D
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 stackoverflow.com/questions/4605118/… . 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.
|
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!!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.