Default Command

DefaultCommand controls which command to initiate form submit when enter key is pressed. Example below demonstrates 3 types of buttons; non-ajax, ajax and standard. Multiple defaultCommand in same form is supported with scope option.

  • btn1
  • btn2
  • btn3

Source

<h:form id="form">
                
    <p:growl autoUpdate="true"/>

    <h:panelGrid columns="2">
        <h:outputLabel for="btnSelect" value="Default Button:" />
        <p:selectOneMenu id="btnSelect" value="#{defaultCommandBean.btn}" onchange="$('#form').submit()">
            <f:selectItems value="#{defaultCommandBean.buttons}" />
        </p:selectOneMenu>
    </h:panelGrid>

    <p:separator />

    <h:panelGrid columns="3" cellpadding="5">
        <h:outputLabel for="name" value="Name:" style="font-weight:bold"/>
        <p:inputText id="name" value="#{defaultCommandBean.text}" />
        <h:outputText value="#{defaultCommandBean.text}" id="display" />
    </h:panelGrid>

    <p:commandButton value="Button1" id="btn1" actionListener="#{defaultCommandBean.btn1Submit}" ajax="false"/>
    <p:commandButton value="Button2" id="btn2" actionListener="#{defaultCommandBean.btn2Submit}" />
    <h:commandButton value="Button3" id="btn3" actionListener="#{defaultCommandBean.btn3Submit}" />

    <p:defaultCommand target="#{defaultCommandBean.btn}" />

</h:form>