How to execute javascript method from server side?

强颜欢笑 提交于 2019-12-24 04:32:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!