My code:
As a generic approach you could customize the button renderer.
I use this renderer for a PrimeFaces p:commandButton
:
public class CommandButtonSingleClickRenderer extends CommandButtonRenderer {
@Override
protected void encodeMarkup(FacesContext context, CommandButton button) throws IOException {
if (isEligible(button)) {
String widgetVar = button.resolveWidgetVar(context);
String onClick = getAttributeValue(context, button, "onclick");
String onComplete = getAttributeValue(context, button, "oncomplete");
button.setOnclick(prefix(onClick, getToggleJS(widgetVar, false)));
button.setOncomplete(prefix(onComplete, getToggleJS(widgetVar, true)));
}
super.encodeMarkup(context, button);
}
protected boolean isEligible(final CommandButton button) {
return button.isAjax()
&& button.isRendered()
&& button.getActionExpression() != null
&& !button.isDisabled()
&& !isConfirmation(button);
}
protected boolean isConfirmation(final CommandButton button) {
String styleClass = button.getStyleClass();
return styleClass != null && styleClass.contains("ui-confirmdialog");
}
protected String getToggleJS(final String widgetVar, final boolean enabled) {
return String.format("var w=PF('%s');if(w){w.%sable();};", widgetVar, enabled ? "en" : "dis");
}
protected String getAttributeValue(final FacesContext context, final CommandButton button, final String attribute) {
ValueExpression ve = button.getValueExpression(attribute);
return ve == null ? null : (String) ve.getValue(context.getELContext());
}
protected String prefix(final String base, final String prefix) {
return base == null ? prefix : prefix + base;
}
}
faces-config.xml:
org.primefaces.component
org.primefaces.component.CommandButtonRenderer
com.whatever.CommandButtonSingleClickRenderer
This renderer was added to PrimeFaces extensions 8.0. If you are using PFE, you can simply add this renderer to your faces-config.xml render-kit
section:
org.primefaces.component
org.primefaces.component.CommandButtonRenderer
org.primefaces.extensions.renderer.CommandButtonSingleClickRenderer