问题
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:
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");
来源:https://stackoverflow.com/questions/17711409/how-to-execute-javascript-method-from-server-side