如何从JSF Bean调用JavaScript函数? [英] How would I call a JavaScript function from a JSF Bean?
问题描述
我需要从Java代码中执行此操作,我尝试了以下操作,但被卡住了:
UIOutput js =新的UIOutput();js.setRendererType("javax.faces.resource.Script");js.getAttributes().put("library","js");js.setValue("alert(123);");FacesContext context = FacesContext.getCurrentInstance();context.getViewRoot().addComponentResource(context,js,"body");
我在服务器日志中没有收到此错误,但是也没有看到警报.
有什么主意吗?
编辑:我尝试了建议的解决方案,即所谓的重复项,但也没有收到服务器错误,但也没有看到任何警报.
我假设您想从JSF ManagedBean(而不是JSF组件)执行此操作.
我不确定对此是否有简单的" jsf解决方案(意味着不添加其他框架或创建和维护一些特定的Java代码).
至少有2种解决方案(我曾使用过):
-
Ajax.onComplete("alert('Hi"))
来自 OmniFaces实用程序框架 - RequestContext.getCurrentInstance().execute("alert('Hi")")来自 PrimeFaces
我确定所有其他组件套件(RichFaces,IceFaces,OpenFaces等)都有一些类似的解决方案.
如果您想继续使用纯JSF,我会继续使用OmniFaces.通常,这是一个很好的补充,它会阻止您维护有关实现细节的知识.
我刚刚找到了纯jsf解决方案 >
I need to do this from Java code, I was trying the following, but got stuck:
UIOutput js = new UIOutput();
js.setRendererType("javax.faces.resource.Script");
js.getAttributes().put("library", "js");
js.setValue("alert(123);");
FacesContext context = FacesContext.getCurrentInstance();
context.getViewRoot().addComponentResource(context, js, "body");
I didn't get an error with this in the server log, but also didn't see the alert.
Any idea?
EDIT: I tried the suggested solution, the alleged duplicate, and I didn't get a server error either, but neither did I see any alert..
I'm assuming you want to do this from a JSF ManagedBean, not a JSF Component.
I'm not sure there is a 'plain' jsf solution for this (meaning not adding another framework or creating and maintaining some specific java code).
There are at least 2 solutions (the ones I worked with):
Ajax.onComplete("alert('Hi'')")
from the OmniFaces utility framework- RequestContext.getCurrentInstance().execute("alert('Hi'')") from PrimeFaces
I'm sure all other component suites (RichFaces, IceFaces, OpenFaces, ...) have some similar solution.
If you want to stay using plain JSF, I'd go for using OmniFaces. It's a great addition in general and prevents you from maintaining knowledge about implementation specifics.
Edit: I just found a plain jsf solution
这篇关于如何从JSF Bean调用JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!