2

in primefaces there's the RequestContext class which add the ability to invoke javascript method from server side :

RequestContext context = RequestContext.getCurrentInstance();          
        //execute javascript oncomplete  
        context.execute("PrimeFaces.info('Hello from the Backing Bean');"); 

i was wondering if it's possible to accomplish that with jsf 2 without using primefaces, please advise.

1

1 Answer 1

3

The easiest but also hackiest way would be to set a property which represents the content of a <h:outputScript> and ajax-update it by adding its client ID to PartialViewContext#getRenderIds().

So, given a

<h:panelGroup id="script"><h:outputScript>#{bean.script}</h:outputScript></h:panelGroup>

you can ajax-update it with

script = "alert('peek-a-boo')";
FacesContext.getCurrentInstance().getPartialViewContext().getRenderIds().add("script");
Sign up to request clarification or add additional context in comments.

6 Comments

will it work fine if the script code moved to client side so that the server side will have only the update for the panelgroup ?
I don't understand what you're trying to say there. Please elaborate the concrete functional requirement. What problem exactly is it you're currently trying to solve and for which you thought that the answer on this question would be the solution?
i want to accomplish the same behavior as in the primefaces requestcontext.
Then you'd have to homegrow a custom PartialViewContext. If you happen to use/know OmniFaces, look at OmniPartialViewContext. It does basically the same and allows executing scripts by Ajax.oncomplete("alert('peek-a-boo')"). See also showcase.omnifaces.org/contexts/OmniPartialViewContext for links to examples and source code.
any ideas how above code can be adapted to be used in jsf 1 ?
|

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.