<p:commandlink>和<p:commandbutton>之间不一致的行为</p:commandbutton> </p:commandlink>

时间:2013-01-04 19:08:20

标签: jsf primefaces commandlink commandbutton

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

  <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链接具有完全相同的操作,处理,更新和不完整属性。但是,链接按预期工作,按钮不执行任何操作。查看生成的标记:

<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,其中包含链接。我错过了什么,或者这是一个错误的表面?如果有错误,除了使用带样式的链接以使它们看起来像按钮之外,还有其他解决方法吗?

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

1 个答案:

答案 0 :(得分:3)

ARGH ...简单的错误。 p:commandButton的type属性决定了行为。似乎通过设置type='button'我已将其变为a(取自primefaces用户指南):

  

按下按钮

     

按钮用于执行自定义javascript   不会导致ajax / non-ajax请求。创建按钮组   输入“按钮”。

     

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

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