Using an Action Method and the Default ActionListener

The UICommand component supports method bindings for two types of methods: action methods and action listener methods. Either type can be used to process an ActionEvent, but the action method type is the most commonly used. An action method has no parameters and returns a String value called the action outcome. The outcome is typically “success” if everything went as planned, and “error” or “failure” if there was a problem. The outcome value can affect which view is displayed next, which we get back to in Chapter 9. In a JSP page, you can use the action attribute with a method binding expression to bind a UICommand component to an action method, as shown in Example 8-1.

As you know from the earlier discussions, an ActionEvent is handled by a listener that implements the ActionListener interface, so there must be something else going on to invoke the action method. The missing piece is called the default ActionListener. This is a listener that is provided by the JSF implementation[1] to make it easier to handle events. When a UICommand component is asked to fire an ActionEvent, it first notifies all regular listeners attached to the component, if any. Then it checks if it’s been configured with an action method binding. If so, it creates an instance of the default ActionListener and asks it to handle the event. The default ActionListener evaluates the action method binding and invokes the method. As hinted at earlier, the default ...

Get JavaServer Faces now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.