DefaultCommand controls which command to initiate form submit with when enter key is pressed. Example below demonstrates 3 types of buttons; non-ajax, ajax and standard.
<div class="card">
<h:form id="form">
<p:growl>
<p:autoUpdate/>
</p:growl>
<h:panelGrid columns="2" cellpadding="7" styleClass="mb-3">
<h:outputLabel for="btnSelect" value="Default Button:"/>
<p:selectOneMenu id="btnSelect" value="#{defaultCommandView.btn}" onchange="$('#form').submit()">
<f:selectItems value="#{defaultCommandView.buttons}"/>
</p:selectOneMenu>
<h:outputLabel for="name" value="Name:" />
<p:inputText id="name" value="#{defaultCommandView.text}"/>
</h:panelGrid>
<p:commandButton value="Button1" id="btn1" action="#{defaultCommandView.btn1Submit}" ajax="false" styleClass="mr-2" />
<p:commandButton value="Button2" id="btn2" action="#{defaultCommandView.btn2Submit}" styleClass="mr-2" />
<h:commandButton value="Button3" id="btn3" action="#{defaultCommandView.btn3Submit}"/>
<p:defaultCommand target="#{defaultCommandView.btn}"/>
</h:form>
</div>
package org.primefaces.showcase.view.misc;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import java.util.ArrayList;
import java.util.List;
import io.quarkus.runtime.annotations.RegisterForReflection;
@Named
@RequestScoped
@RegisterForReflection(serialization = true)
public class DefaultCommandView {
private String text;
private String text2;
private String btn = "btn1";
private List<String> buttons;
@PostConstruct
public void init() {
buttons = new ArrayList<>();
buttons.add("btn1");
buttons.add("btn2");
buttons.add("btn3");
}
public String getBtn() {
return btn;
}
public void setBtn(String btn) {
this.btn = btn;
}
public List<String> getButtons() {
return buttons;
}
public void setButtons(List<String> buttons) {
this.buttons = buttons;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getText2() {
return text2;
}
public void setText2(String text2) {
this.text2 = text2;
}
public void addMessage(String btn) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Submitted with " + btn));
}
public void btn1Submit() {
addMessage("btn1");
}
public void btn2Submit() {
addMessage("btn2");
}
public void btn3Submit() {
addMessage("btn3");
}
}