Use a4j:actionListener to register an ActionListener class on a parent action component. Multiple listener methods can be registered on an action components in this way.
The a4j:actionListener tag differs from the standard JSF tag in that it allows the listener to be attached in any of three ways:
<!DOCTYPE html> <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich"> <h:form id="form"> <h:panelGrid columns="3"> <h:commandButton value="Invoke listener method"> <a4j:actionListener listener="#{actionListenerBean.handleActionMethod}"/> <f:ajax render="messages"/> </h:commandButton> <h:commandButton value="Invoke listener by type"> <a4j:actionListener type="org.richfaces.demo.actionListener.ActionListenerBean$ActionListenerImpl"/> <f:ajax render="messages"/> </h:commandButton> <h:commandButton value="Invoke listener by binding"> <a4j:actionListener binding="#{actionListenerBean.actionListener}"/> <f:ajax render="messages"/> </h:commandButton> </h:panelGrid> <fieldset> <legend>Messages</legend> <h:messages id="messages"/> </fieldset> </h:form> </ui:composition>
package org.richfaces.demo.actionListener; import javax.faces.application.FacesMessage; import javax.faces.bean.ManagedBean; import javax.faces.bean.NoneScoped; import javax.faces.context.FacesContext; import javax.faces.event.AbortProcessingException; import javax.faces.event.ActionEvent; import javax.faces.event.ActionListener; /** * @author Nick Belaevski * */ @ManagedBean @NoneScoped public class ActionListenerBean { public static final class ActionListenerImpl implements ActionListener { public void processAction(ActionEvent event) throws AbortProcessingException { addFacesMessage("Implementation of ActionListener created and called: " + this); } } private static final class BoundActionListener implements ActionListener { public void processAction(ActionEvent event) throws AbortProcessingException { addFacesMessage("Bound listener called"); } } private ActionListener actionListener = new BoundActionListener(); private static void addFacesMessage(String messageText) { FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage(messageText)); } public void handleActionMethod(ActionEvent event) throws AbortProcessingException { addFacesMessage("Method expression listener called"); } public void handleActionMethodComposite(ActionEvent event) throws AbortProcessingException { addFacesMessage("Method expression listener called from composite component"); } public ActionListener getActionListener() { return actionListener; } }