JSF comandButton执行动作,然后js函数以bean值作为参数

时间:2018-03-02 15:33:42

标签: javascript jsf primefaces commandbutton

我从JSF开始,所以看起来非常基本。 我有一个命令按钮,需要调用一个方法来计算bean值,我希望新值作为参数传递给JS函数。 我的第一次尝试是(我需要在我的js函数中使用新的信息值):

    <h:commandButton  id="searchbtn" value="refresh" 
                     action="#{controller.computeBean}"
                     onclick="renderGraph('#{screenBean.info}');" >
        <f:ajax execute="@this" render="@this"   />
    </h:commandButton>

成功调用控制器。刷新按钮以更新参数。 但onClick操作发生在控制器操作之前,因此bean尚未呈现。所以我不得不在按钮上单击两次以获得所需的输出。 所以我使用了来自primeface的oncomplete

    <p:commandButton  value="#refreshPF" 
                     action="#{controller.computeBean}"
                     oncomplete="renderGraph('#{screenBean.info}');"
                     update="@this">

    </p:commandButton>

但是现在在js函数中传递的bean不会使用新值更新。我知道该动作被调用,因为我在执行控制器方法结束时打印了bean值。 我不确定我做错了什么,但我猜它来自更新。 我有人可能会提供一些帮助,我会非常感激。

使用的图书馆: jsf 2.2.13和 primeface 6.1

1 个答案:

答案 0 :(得分:0)

您可以从Java调用JS代码,这在呈现响应时执行。查看:https://www.primefaces.org/showcase/ui/misc/requestContext.xhtml

在动作方法Java中你可以编码:

PrimeFaces.current().executeScript("alert('This onload script is added from backing bean.')");