p:commandButton 与 h:commandButton

2024-01-04

当我使用 PrimeFaces 时p:commandButton

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

我没有看到输入的验证消息(都是默认的h:或 PrimeFacesp:)例如

<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>

就像@Partlov 在问题下面的评论中写道的那样,

主要区别在于 p:commandButton 默认情况下是 AJAX,而 h:commandButton 默认情况下是非 AJAX。

So

<p:commandButton ... />

更像是

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

but

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

是错误的并导致未定义的行为

或者反过来

<h:commandButton ... />

is like

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

The p:commandButton将默认提交表单。然而,默认情况下,ajax 调用完成后,它不会更新表单中的任何内容,因此不会显示消息(在开发模式下,日志文件中会显示消息已排队但未显示)。非ajaxh: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>

添加一个(多余的)f:ajax or p:ajax里面一个p:commandXXX可能会导致奇怪的未定义行为

See also

  • 了解 PrimeFaces 进程/更新和 JSF f:ajax 执行/渲染属性 https://stackoverflow.com/questions/25339056/understanding-primefaces-process-update-and-jsf-fajax-execute-render-attributes
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

p:commandButton 与 h:commandButton 的相关文章