O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using an Action Listener Method or Instance

In most cases, you don’t need access to the actual ActionEvent in order to do the right thing, because each component can be bound to a separate action method that implements component-specific behavior. The exception is when one method needs to handle events fired by multiple components, but that turns out to be a very rare case. In fact, there’s nothing in the sample application that requires this, and I’m hard pressed to come up with any good use case for it. Even so, the JSF specification supports two alternatives to the action method that provides access to the ActionEvent, in case you do need it.

The first alternative is the action listener method binding, defined by the actionListener attribute in a JSP page:

<h:commandButton value="Add"
  actionListener="#{entryHandler.handleAdd}" />

An action listener method has the exact same signature as the ActionListener processAction() method we looked at earlier; it takes an ActionEvent argument and has a void return type:

    public void processAction(ActionEvent e) throws AbortProcessingException {
        UIComponent myCommand = e.getComponent( );
        // Do whatever you need to do depending on which component fired
        // the event
        ...
    }

Any object available as a JSF EL expression variable can implement the action listener method, but you can bind a component only to one action listener method. If you need to use more than one listener to handle the same event—maybe one that logs the event and another that really ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required