【问题标题】:How make commandButton not fully refresh page? How to use f:ajax?如何使 commandButton 不完全刷新页面?如何使用 f:ajax?
【发布时间】:2012-02-17 19:33:06
【问题描述】:

我有一个按钮来提交表单并调用托管 bean 操作。

<h:form>
    ...
    <h:commandButton value="Submit" action="#{bean.submit}" />
</h:form>

但是当我按下按钮时,它会刷新整个页面,有时还会更改 URL。

有什么方法可以不刷新页面并仍然调用操作?

【问题讨论】:

  • 为了您的考虑,您可能需要查看Primefaces PhotoCam 组件。我相信它使用 HTML5 而不是 Flash。
  • Primefaces PhotoCam 在我看来确实是基于 Flash 的(这是明智的,因为没有一个主流浏览器支持通过 HTML(5)/JavaScript 访问相机,据我所知只有 Opera 目前支持它)。
  • PF photoCam 现在在可用的情况下使用本机访问

标签: jsf refresh commandbutton


【解决方案1】:

利用 Ajax。这是在感兴趣的命令按钮内嵌套&lt;f:ajax&gt; 的问题。

<h:form>
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="@none" />
    </h:commandButton>
</h:form>

特别是render="@none"(无论如何这是默认值,您可以完全省略该属性)将指示 JSF 在提交后重新渲染任何内容。如果您打算只重新渲染特定组件而不是整个页面,那么您还可以在 render 属性中指定该特定组件的 ID。

<h:form>
    ...
    <h:commandButton ...>
        <f:ajax execute="@form" render="result" />
    </h:commandButton>
    ...
    <h:panelGroup id="result">...</h:panelGroup>
</h:form>

如果您已经在使用 PrimeFaces,那么只需使用 &lt;p:commandButton&gt; 而不是 &lt;h:commandButton&gt;。默认情况下它已经使用 ajax,因此您不需要嵌套在 &lt;f:ajax&gt; 中。您只需要记住使用属性名称process 和update 而不是execute 和render。

<h:form>
    ...
    <p:commandButton ... update="result" />
    ...
    <h:panelGroup id="result">...</h:panelGroup>
</h:form>

execute 属性已经默认为@form,因此可以省略。

另见:

【讨论】:

  • 感谢@BalusC。我认为 是默认的。但如果没有 ajax 标签,它就无法工作。知道为什么吗?
  • @eskalera:您将标准 JSF 与 PrimeFaces 混淆了。
猜你喜欢
  • 1970-01-01
  • 2012-10-03
  • 2016-09-18
  • 2011-07-21
  • 1970-01-01
  • 2011-01-02
  • 2021-12-04
相关资源
最近更新 更多