Calling a JavaScript function from managed bean

2019-01-03 00:03发布

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

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

5条回答
贼婆χ
2楼-- · 2019-01-03 00:18

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

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

In PrimeFaces 6.2 and up:

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.

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

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

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.

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

See also:

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-03 00:19

You can't simply.

Managed Bean works on server and JavaScript on browser.

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

查看更多
太酷不给撩
4楼-- · 2019-01-03 00:22

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.

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.

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

查看更多
该账号已被封号
5楼-- · 2019-01-03 00:33

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

From the Primefaces 3.4 documentation:

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;

Code

public void save() {
  RequestContext requestContext = RequestContext.getCurrentInstance();  
  requestContext.execute("dialog.hide()");
}
查看更多
Fickle 薄情
6楼-- · 2019-01-03 00:36

Closest thing in Primefaces is;

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

Having said there is also an enhancement in 3.0;

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

查看更多
登录 后发表回答