The Java EE 5 Tutorial

Implementing Action Listeners

An ActionListener implementation must include a processAction(ActionEvent) method. The processAction(ActionEvent) method processes the specified action event. The JavaServer Faces implementation invokes the processAction(ActionEvent) method when the ActionEvent occurs.

The Duke’s Bookstore application does not use any ActionListener implementations. Instead, it uses method expressions from actionListener attributes to refer to backing bean methods that handle events. This section explains how to turn one of these methods into an ActionListener implementation.

The chooselocale.jsp page allows the user to select a locale for the application by clicking on one of a set of hyperlinks. When the user clicks one of the hyperlinks, an action event is generated, and the chooseLocaleFromLink(ActionEvent) method of LocaleBean is invoked. Instead of implementing a bean method to handle this event, you can create a listener implementation to handle it. To do this, you do the following:

The listener implementation would look something like this:

public class LocaleChangeListener extends Object implements
     ActionListener {

    private HashMap<String, Locale> locales = null;
     public LocaleChangeListener() {
        locales = new HashMap<String, Locale>(4);
        locales.put("NAmerica", new Locale("en", "US"));
        locales.put("SAmerica", new Locale("es", "MX"));
        locales.put("Germany", new Locale("de", "DE"));
        locales.put("France", new Locale("fr", "FR"));

    public void processAction(ActionEvent event)
        throws AbortProcessingException {
        String current = event.getComponent().getId();
        FacesContext context = FacesContext.getCurrentInstance();

Registering an Action Listener on a Component explains how to register this listener onto a component.