15

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

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

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

有没有办法不刷新页面并仍然调用操作?

4

1 回答 1

24

使用 Ajax。这是嵌套<f:ajax>在感兴趣的命令按钮内的问题。

<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,那么只需使用<p:commandButton>而不是<h:commandButton>. 默认情况下它已经使用了 ajax,所以你不需要嵌套在<f:ajax>. 您只需要记住使用属性名称processandupdate而不是executeand render

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

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

也可以看看:

于 2012-01-24T14:49:29.373 回答