从托管bean调用JavaScript函数 [英] Calling a JavaScript function from managed bean

查看:226
本文介绍了从托管bean调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从JSF中的托管bean调用(执行)JavaScript函数?

Is there a way to call (execute) a JavaScript function from managed bean in JSF?

如果这是相关的,我也使用PrimeFaces。

If that's relevant, I'm also using PrimeFaces.

推荐答案

在PrimeFaces 6.2之前,您可以使用 RequestContext#execute() 为此。

In PrimeFaces pre 6.2, you can use RequestContext#execute() for this.

public void submit() {
    // ...
    RequestContext.getCurrentInstance().execute("alert('peek-a-boo');");
}

在PrimeFaces 6.2及以上版本中:

In PrimeFaces 6.2 and up:

public void submit() {
    // ...
    PrimeFaces.current().executeScript("alert('peek-a-boo');");
}

在标准JSF中,没有直接的公共API。最好的方法是将所需的脚本设置为bean属性,并在bean属性不为空时有条件地呈现< h:outputScript> 组件。

In standard JSF, there is no direct public API for that. Best what you can get is to set the desired script as a bean property and conditionally render a <h:outputScript> component when the bean property is not empty.

<h:commandButton ... action="#{bean.submit}" />
<h:outputScript rendered="#{not empty bean.script}">#{bean.script}</h:outputScript>



public void submit() {
    // ...
    script = "alert('peek-a-boo');";
}

如果您通过ajax提交表单,请不要忘记将< h:outputScript> 包装在另一个组件中,然后更新ajax-update。另请参见 Ajax更新/渲染不适用于具有呈现属性的组件。

In case you're submitting the form by ajax, don't forget to wrap the <h:outputScript> in another component and ajax-update it instead. See also Ajax update/render does not work on a component which has rendered attribute.

<h:commandButton ... action="#{bean.submit}">
    <f:ajax execute="@form" render="script" />
</h:commandButton>
<h:panelGroup id="script">
    <h:outputScript rendered="#{not empty bean.script}">#{bean.script}</h:outputScript>
</h:panelGroup>

至于没有直接的公共API声明,奇怪的是 PartialResponseWriter class(负责编写JSF ajax响应)已经自JSF 2.0 startEval() endEval() 方法应该使您能够将回调脚本直接写入响应但是在即将推出的JSF 2.3之前, <$中出现了令人惊讶的公共方法。 c $ c> PartialViewContext 将委托给这些方法。根据 issue 1412 PartialViewContext#getEvalScripts()最终被添加到公共API。

As to "there is no direct public API for that" statement, curiously the PartialResponseWriter class (responsible for writing JSF ajax responses) has already since JSF 2.0 startEval() and endEval() methods which should enable you to write callback scripts directly to the response but until upcoming JSF 2.3 there's surprisingly no public method in PartialViewContext which will delegate to those methods. As per issue 1412 PartialViewContext#getEvalScripts() is finally been added to public API.

public void submit() {
    // ...
    FacesContext.getCurrentInstance().getPartialViewContext().getEvalScripts().add("alert('peek-a-boo');");
}

对于较旧的JSF版本,只能通过创建自定义<$来实现c $ c> PartialViewContext 实现。 JSF实用程序库OmniFaces完全使用 OmniPartialViewContext 可以通过 Ajax 实用程序类

For older JSF versions, this can only be implemented by creating a custom PartialViewContext implementation. JSF utility library OmniFaces has done exactly that with OmniPartialViewContext which can be used via Ajax utility class.

public void submit() {
    // ...
    Ajax.oncomplete("alert('peek-a-boo');");
}



参见:



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆