从托管bean调用JavaScript函数

时间:2022-04-09 15:59:15

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

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

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

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

5 个解决方案

#1


33  

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

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

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

In PrimeFaces 6.2 and up:

在PrimeFaces 6.2及以上版本中:

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

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.

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

<h:commandButton ... action="#{bean.submit}" />
<h:outputScript rendered="#{not empty bean.script}">#{bean.script}</h:outputScript>
public void submit() {
    // ...
    script = "alert('peek-a-boo');";
}

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.

如果您是通过ajax提交表单,请不要忘记将 包装在另一个组件中,而是将其更新为ajax。另请参见Ajax update / render对具有呈现属性的组件不起作用。

<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>

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.

至于“没有直接的公共API”声明,奇怪的是PartialResponseWriter类(负责编写JSF ajax响应)已经有JSF 2.0 startEval()和endEval()方法,它们应该使你能够直接将回调脚本编写到响应,但直到即将到来的JSF 2.3,令人惊讶的是PartialViewContext中没有公共方法将委托给那些方法。根据问题1412 PartialViewContext#getEvalScripts()最终被添加到公共API。

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

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.

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

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

See also:

#2


32  

Depending on which version of Primefaces you're on you can use RequestContext.execute("{js here}");

根据您所使用的Primefaces版本,您可以使用RequestContext.execute(“{js here}”);

From the Primefaces 3.4 documentation:

从Primefaces 3.4文档:

RequestContext provides a way to execute javascript when the ajax request completes, this approach is easier compared to passing callback params and execute conditional javascript. Example below hides the dialog when ajax request completes;

RequestContext提供了一种在ajax请求完成时执行javascript的方法,与传递回调参数和执行条件javascript相比,这种方法更容易。下面的示例在ajax请求完成时隐藏对话框;

Code

public void save() {
  RequestContext requestContext = RequestContext.getCurrentInstance();  
  requestContext.execute("dialog.hide()");
}

#3


7  

Closest thing in Primefaces is;

Primefaces中最接近的是;

http://www.primefaces.org/showcase/ui/callbackParams.jsf

Having said there is also an enhancement in 3.0;

话虽如此,3.0也有所提升;

http://code.google.com/p/primefaces/issues/detail?id=1342

#4


4  

You can't simply.

你不能简单。

Managed Bean works on server and JavaScript on browser.

Managed Bean适用于浏览器上的服务器和JavaScript。

You can make conditionally invoke JavaScript depending on the value set in managedbean

您可以根据managedbean中设置的值有条件地调用JavaScript

#5


1  

In general, Java provides an API to evaluate a string using a scripting engine. This can be accomplished by javax.script.ScriptEngine and javax.script.ScriptEngineManager classes.

通常,Java提供了一个API来使用脚本引擎来评估字符串。这可以通过javax.script.ScriptEngine和javax.script.ScriptEngineManager类来完成。

I am not entirely sure what your situation is, but if you can pass the javascript as a string to the managed bean, you could probably use Java scripting API to run the javascript on the server side.

我不完全确定你的情况,但是如果你可以将javascript作为字符串传递给托管bean,你可以使用Java脚本API在服务器端运行javascript。

For more information, check out this link: http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

有关更多信息,请访问以下链接:http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

#1


33  

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

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

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

In PrimeFaces 6.2 and up:

在PrimeFaces 6.2及以上版本中:

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

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.

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

<h:commandButton ... action="#{bean.submit}" />
<h:outputScript rendered="#{not empty bean.script}">#{bean.script}</h:outputScript>
public void submit() {
    // ...
    script = "alert('peek-a-boo');";
}

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.

如果您是通过ajax提交表单,请不要忘记将 包装在另一个组件中,而是将其更新为ajax。另请参见Ajax update / render对具有呈现属性的组件不起作用。

<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>

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.

至于“没有直接的公共API”声明,奇怪的是PartialResponseWriter类(负责编写JSF ajax响应)已经有JSF 2.0 startEval()和endEval()方法,它们应该使你能够直接将回调脚本编写到响应,但直到即将到来的JSF 2.3,令人惊讶的是PartialViewContext中没有公共方法将委托给那些方法。根据问题1412 PartialViewContext#getEvalScripts()最终被添加到公共API。

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

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.

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

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

See also:

#2


32  

Depending on which version of Primefaces you're on you can use RequestContext.execute("{js here}");

根据您所使用的Primefaces版本,您可以使用RequestContext.execute(“{js here}”);

From the Primefaces 3.4 documentation:

从Primefaces 3.4文档:

RequestContext provides a way to execute javascript when the ajax request completes, this approach is easier compared to passing callback params and execute conditional javascript. Example below hides the dialog when ajax request completes;

RequestContext提供了一种在ajax请求完成时执行javascript的方法,与传递回调参数和执行条件javascript相比,这种方法更容易。下面的示例在ajax请求完成时隐藏对话框;

Code

public void save() {
  RequestContext requestContext = RequestContext.getCurrentInstance();  
  requestContext.execute("dialog.hide()");
}

#3


7  

Closest thing in Primefaces is;

Primefaces中最接近的是;

http://www.primefaces.org/showcase/ui/callbackParams.jsf

Having said there is also an enhancement in 3.0;

话虽如此,3.0也有所提升;

http://code.google.com/p/primefaces/issues/detail?id=1342

#4


4  

You can't simply.

你不能简单。

Managed Bean works on server and JavaScript on browser.

Managed Bean适用于浏览器上的服务器和JavaScript。

You can make conditionally invoke JavaScript depending on the value set in managedbean

您可以根据managedbean中设置的值有条件地调用JavaScript

#5


1  

In general, Java provides an API to evaluate a string using a scripting engine. This can be accomplished by javax.script.ScriptEngine and javax.script.ScriptEngineManager classes.

通常,Java提供了一个API来使用脚本引擎来评估字符串。这可以通过javax.script.ScriptEngine和javax.script.ScriptEngineManager类来完成。

I am not entirely sure what your situation is, but if you can pass the javascript as a string to the managed bean, you could probably use Java scripting API to run the javascript on the server side.

我不完全确定你的情况,但是如果你可以将javascript作为字符串传递给托管bean,你可以使用Java脚本API在服务器端运行javascript。

For more information, check out this link: http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html

有关更多信息,请访问以下链接:http://docs.oracle.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html