【问题标题】:How to execute h:commandButton actionListener before onclick?如何在 onclick 之前执行 h:commandButton actionListener?
【发布时间】:2012-07-27 12:41:43
【问题描述】:

下面的代码我需要一些帮助

                <h:commandButton  id="WhatifButtonID" value="#{demandBean.dmdReviewScreenLabelVO.whatIf}" style="width:60px; height:22px;"  actionListener="#{demandBean.whatif}"  onclick="window.open('DemandTargetList.xhtml','whatif','width=460,height=280,top=150,left=350,resizable=no,scrollbars=no')" >
                    <f:ajax execute="@this" ></f:ajax>
                </h:commandButton>

在上述情况下,首先执行 onclick 属性,然后执行 actionlistener 属性。我希望先执行 actionListener 属性,然后执行 onclick 函数,因为加载 onclick 的页面需要从 actionListener 获取某些值。请让我知道如何实现同样的目标。提前致谢。

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:

    &lt;f:ajax&gt; 完成一个脚本。

    <h:commandButton value="#{demandBean.dmdReviewScreenLabelVO.whatIf}" actionListener="#{demandBean.whatif}">
        <f:ajax execute="@this" render="popupScript" />
    </h:commandButton>
    <h:panelGroup id="popupScript">
        <h:outputScript rendered="#{demandBean.whatifPressed}">
            window.open('DemandTargetList.xhtml','whatif','width=460,height=280,top=150,left=350,resizable=no,scrollbars=no');
        </h:outputScript>
    </h:panelGroup>
    

    您在 whatif() 方法中将 whatifPressed 设置为 true

    【讨论】:

    • 非常感谢 BalusC 的快速回复。它真的很有帮助。但是加载弹出窗口所需的时间似乎更多。您还可以让我知道执行属性的顺序吗?我如何知道哪个属性首先被执行。
    • 如果您了解 HTML/JS 的工作原理(因为这基本上是 JSF 生成的全部内容),这很容易。因此,通过一个体面的 HTML/JS 教程,然后查看 JSF 生成的 HTML/JS 代码(在浏览器中右键单击页面并执行 查看源代码)应该就足够清楚了。
    【解决方案2】:

    PrimeFaces 可以通过使用&lt;p:remoteCommand/&gt; 标签来帮助解决这个问题。然后,您可以只使用一个普通的旧按钮来调用 remoteCommand,然后只需执行 window.open()。

    <p:remoteCommand name="viewSomething" actionListener="#{someActionIWantToExecuteBeforeOpeningWindow}" update="@this" process="@this"/>
    <button onclick="viewSomething(); window.open('http://somewhere.com', 'windowName', 'height=924,width=723');">Click me!</button>
    

    如果您调用 javascript 函数,它会创建一些独特的东西,您可以在项目列表中使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-14
      • 2012-11-24
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      相关资源
      最近更新 更多