【问题标题】:p:commandButton vs. h:commandButtonp:commandButton 与 h:commandButton
【发布时间】:2013-03-07 09:51:42
【问题描述】:

当我使用 PrimeFaces p:commandButton

<p:commandButton action=#{bean.action} />

我没有看到输入的验证消息(默认的 h: 或 PrimeFaces p: 的)。例如

<f:validateRequired />

当我使用默认命令按钮时

<h:commandButton action=#{bean.action} />

我确实看到了验证。造成这种差异的原因是什么?

我正在使用 Prime Faces 3.5 和 Mojarra 2.1.18

<h:form id="reliefhourheadcopy-form">

        <h:panelGrid columns="1">
            <h:outputText value="Kopiere Entlastungsstunden von" />
            <h:outputText value="Semester: #{reliefHourHeadManagedBean.reliefHourHead.semester}" />
            <h:outputText value="Jahr: #{reliefHourHeadManagedBean.reliefHourHead.year}" />
            <h:outputText value="nach" />               
        </h:panelGrid>

        <h:panelGrid columns="3">

            <h:outputText value="Semester:" />
            <p:selectOneMenu id="semester" value="#{reliefHourHeadManagedBean.semester}">
                <f:selectItems value="#{reliefHourHeadManagedBean.semesterTypes}" />
            </p:selectOneMenu>
            <h:message for="semester" />

            <h:outputText for="yearSpinner" value="Jahr:" />
            <p:spinner id="yearSpinner" value="#{reliefHourHeadManagedBean.year}" maxlength="4" min="2000" max="2030" size="4">
                <f:validateRequired />
                <f:validateLongRange minimum="2000" maximum="2030" />
            </p:spinner>
            <h:message for="yearSpinner" />

        </h:panelGrid>

        <h:panelGrid columns="1" style="margin-top:25px">
            <p:commandButton action="#{reliefHourHeadManagedBean.copyReliefHourHead}" value="Kopieren" icon="ui-icon-copy" >
                <f:param name="reliefhourhead_id" value="#{reliefHourHeadManagedBean.reliefHourHeadId}" />
            </p:commandButton>
        </h:panelGrid>

    </h:form>

【问题讨论】:

  • 主要区别在于p:commandButton默认为AJAX,h:commandButton默认为非AJAX。整个表格的邮政编码。也许您只是没有使用 primefaces 按钮更新验证消息字段。
  • update="@form" 添加到p:commandButton 并查看是否显示错误消息。
  • 感谢 partlov,这是我需要的提示! update="@form" 可以解决问题。但也有 ajax="false"。如果我使用 ajax="false" , 也会正确更新。问候卡隆。
  • 是的,在这种情况下,非 AJAX 请求按钮将充当h:commandButton
  • @parlov:你想创建一个包含所有这些信息的答案吗?赞成的答案在解释事情方面并不是那么好。否则我想写一个包含所有细节的答案。

标签: jsf primefaces commandbutton


【解决方案1】:

就像@Partlov 在问题下方的 cmets 中所写,

主要区别在于 p:commandButton 默认为 AJAX,而 h:commandButton 默认为非 AJAX。

所以

<p:commandButton ... />

更像

<h:commandButton ...>
    <f:ajax/>
</h:commandButton>

但是

<p:commandButton ...>
    <p:ajax/>
</p:commandButton>

错误并导致未定义的行为

反之亦然

<h:commandButton ... />

就像

<p:commandButton ajax="false" ... />

p:commandButton 默认会提交表单。但是,默认情况下,它不会在 ajax 调用完成后更新表单中的任何内容,因此不会显示消息(在开发模式下,日志文件中会显示消息已入队但未显示)。非 ajax h:commandButton 会进行整页刷新以显示消息。为了在使用p:commandButton 时更新表单(包含消息组件),您需要添加update 属性:

<p:commandButton action="#{reliefHourHeadManagedBean.copyReliefHourHead}" value="Kopieren" icon="ui-icon-copy" update="@form">
    <f:param name="reliefhourhead_id" value="#{reliefHourHeadManagedBean.reliefHourHeadId}" />
</p:commandButton>

p:commandXXX 中添加(多余的)f:ajaxp:ajax 可能会导致奇怪的未定义行为

另请参阅

【讨论】:

  • 它会提交整个表单,只是不会更新整个表单。
  • 你的意思是不更新就会出现这种情况?如果是这种情况,我将编辑我的答案。
  • 通过更新,您可以控制要更新的部分,通过流程,您可以控制提交的内容。通过此更新,您可以更新表单(带有消息,因此消息将可见)。如果没有更新,您将看不到消息。
  • 谢谢你,在我以前在 primefaces 中混合更新和处理之前!我会编辑这个答案。
猜你喜欢
  • 2013-02-02
  • 2014-12-26
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
相关资源
最近更新 更多