【问题标题】:JSF onclick refreshes pageJSF onclick 刷新页面
【发布时间】:2014-05-19 10:36:41
【问题描述】:
<h:commandButton  onclick="window.open('/page.html')" value="test" action="#{bean.myAction}"></h:commandButton>

点击上方后,浏览器会刷新页面并滚动到顶部。

如果我使用“return false;”操作永远不会运行。

我怎样才能阻止这种情况发生?

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    不确定您为什么使用"window.open('/page.html')",但是如果您想在操作命令完成后打开页面,请执行以下操作

    <h:commandButton value="test" action="#{bean.myAction}">
        <f:ajax onevent="function(data){ if(data.status === 'success') window.open('/page.html')}"/>
    </h:commandButton>
    

    【讨论】:

      【解决方案2】:
      <h:commandButton onclick="window.open('/page.html')" value="test" action="#{bean.myAction}">
          <f:ajax execute="@this" render="@none" />
      </h:commandButton>
      

      应该在没有 PrimeFaces 的情况下做到这一点;)

      【讨论】:

        【解决方案3】:

        它确实刷新了页面,为什么不呢? h:commandButton 充当简单的表单提交。您需要 ajax 调用才能不重新加载整个页面。

        例如,您可以查看 primefaces component library,它已准备好使用支持完全 ajax 调用的组件 - 请参阅 &lt;p:commandButton&gt;

        至于return false,如果您在onclick 中使用它,它不会运行操作,因为它会阻止表单提交,因此简化了“不通过bean”。

        【讨论】:

          猜你喜欢
          • 2012-08-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 1970-01-01
          • 2012-09-18
          • 1970-01-01
          相关资源
          最近更新 更多