【问题标题】:Inconsistent behavior between <p:commandLink> and <p:commandButton><p:commandLink> 和 <p:commandButton> 之间的行为不一致
【发布时间】:2013-01-04 19:08:20
【问题描述】:

我有一个删除确认对话框:

  <p:confirmDialog id="deleteConfirmDialog"
    header="#{i18n['confirm-deletion']}"
    widgetVar="pnp_delete_confirmDialog" closeable="false"
    appendToBody="true">
    <f:facet name="message">
      <h:outputFormat id="deleteConfirmMessage"
        value="#{i18n['confirm-subscription-deletion']}">
        <f:param value="#{subscriptions.deleteSelected.title}" />
      </h:outputFormat>
    </f:facet>
    <h:form id="pnpConfirmDeleteForm">
      <p:commandButton id="deleteActionConfirmed"
        action="#{subscriptions.delete}" value="#{i18n['delete']}"
        type="button" process="@this" update=":pnpEditForm :pnpExistingForm"
        oncomplete="pnp_delete_confirmDialog.hide()" />
      <p:commandLink id="deleteActionConfirmedLink"
        action="#{subscriptions.delete}" process="@this"
        update=":pnpEditForm :pnpExistingForm"
        oncomplete="pnp_delete_confirmDialog.hide()">#{i18n['delete']}</p:commandLink>
      <p:commandButton id="deleteActionCancelled" value="#{i18n['cancel']}"
        type="button" onclick="pnp_delete_confirmDialog.hide()" />
    </h:form>
  </p:confirmDialog>

您会注意到deleteActionConfirmed 按钮和deleteActionConfirmedLink 链接具有完全相同的action、process、update 和oncomplete 属性。但是,链接按预期工作,按钮什么也不做。查看生成的标记:

<form enctype="application/x-www-form-urlencoded"
  action="/primefaces-quickstart/xhtml/pnp-configuration.xhtml" method="post"
  name="pnpConfirmDeleteForm" id="pnpConfirmDeleteForm">
  <input type="hidden" value="pnpConfirmDeleteForm" name="pnpConfirmDeleteForm">
  <button type="button"
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
    name="pnpConfirmDeleteForm:deleteActionConfirmed"
    id="pnpConfirmDeleteForm:deleteActionConfirmed" role="button"
    aria-disabled="false">
    <span class="ui-button-text">Delete</span>
  </button>
  <a
    onclick="PrimeFaces.ab({source:'pnpConfirmDeleteForm:deleteActionConfirmedLink',process:'pnpConfirmDeleteForm:deleteActionConfirmedLink',update:'pnpEditForm pnpExistingForm',oncomplete:function(xhr,status,args){pnp_delete_confirmDialog.hide();}});return false;"
    class="ui-commandlink" href="#"
    id="pnpConfirmDeleteForm:deleteActionConfirmedLink">Delete</a>
  <button type="button" onclick="pnp_delete_confirmDialog.hide();"
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
    name="pnpConfirmDeleteForm:deleteActionCancelled"
    id="pnpConfirmDeleteForm:deleteActionCancelled" role="button"
    aria-disabled="false">
    <span class="ui-button-text">Cancel</span>
  </button>
  <input type="hidden" autocomplete="off"
    value="7076449462381240234:-2976764560029615800" id="javax.faces.ViewState"
    name="javax.faces.ViewState">
</form>

你可以看到没有为按钮生成onclick,那里有链接。我错过了什么还是这是primefaces中的错误?如果是错误,除了使用带有样式的链接使它们看起来像按钮之外,还有其他解决方法吗?

我在 tomcat 6.0.32(servlet 2.5,el 2.1)上使用 mojarra 2.1.14 和 primefaces 3.4.2。

【问题讨论】:

    标签: jsf primefaces commandlink commandbutton


    【解决方案1】:

    啊……简单的错误。 p:commandButton 的类型属性决定了行为。看来,通过设置 type='button' 我已经把它变成了(取自 primefaces 用户指南):

    按钮

    按钮用于执行自定义 javascript 不会引起 ajax/非 ajax 请求。创建按钮组 键入“按钮”。

    <p:commandButton type="button" value="Alert" onclick="alert(‘Prime’)" />

    切换提交类型修复了这个问题。经验教训...如果使用按钮,type='button' 会导致组件忽略任何服务器端 action

    【讨论】:

    • 或者只是删除type 属性。它已经默认为正确的了。
    • @BalusC,这实际上是我所做的。感谢您的输入。如果type='button'action 属性会被忽略,这似乎很奇怪。如果它检测到该操作存在并自动更改要提交的类型,或者记录警告或抛出异常表明它不会执行您尝试执行的操作,那就太好了...
    • 必要时向 PF 人员报告。
    猜你喜欢
    • 2012-05-15
    • 2021-07-19
    • 2014-08-31
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多