从托管 bean 调用 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5675017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a JavaScript function from managed bean
提问by Maddy
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。
回答by BalusC
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');");
}
Keep in mind that these have to be used in an ajax call.
请记住,这些必须在 ajax 调用中使用。
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 属性,并<h:outputScript>
在 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 提交表单,请不要忘记将其包装<h:outputScript>
在另一个组件中并使用 ajax-update 代替它。另请参阅Ajax 更新/渲染不适用于具有渲染属性的组件。
<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 was surprisingly no public method in PartialViewContext
which will delegate to those methods. As per issue 1412PartialViewContext#getEvalScripts()
is finally been added to public API.
至于“没有直接的公共 API”声明,奇怪的是PartialResponseWriter
该类(负责编写 JSF ajax 响应)自 JSF 2.0startEval()
和endEval()
方法以来就已经存在,这些方法应该使您能够将回调脚本直接写入响应,但直到即将到来的 JSF 2.3 那里令人惊讶的是,没有PartialViewContext
将委托给这些方法的公共方法。根据issue 1412PartialViewContext#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:
也可以看看:
回答by mbeedub
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()");
}
回答by Cagatay Civici
Closest thing in Primefaces is;
在 Primefaces 中最接近的是;
http://www.primefaces.org/showcase/ui/callbackParams.jsf
http://www.primefaces.org/showcase/ui/callbackParams.jsf
Having said there is also an enhancement in 3.0;
话虽如此,3.0 中也有增强功能;
回答by Jigar Joshi
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
回答by Nikhil
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