Understanding the JSF Event Model

The JSF event model is based on the event model defined by the JavaBeans specification. In this model, an event is represented by an instance of an event class. An event source object fires an event by calling an event notification method on event listener objects registered to receive the event, passing a reference to the event object as a notification method argument.

Let’s look at what this means in more detail. All JSF event classes extend the javax.faces.event.FacesEvent class:

package javax.faces.event;

import java.util.EventObject;
import javax.faces.component.UIComponent;
...

public abstract class FacesEvent extends EventObject {
    public FacesEvent(UIComponent component) {
        super(component);
    }

    public UIComponent getComponent( ) {
        return ((UIComponent) getSource( ));
    }
    ...
}

The FacesEvent class extends the standard Java event superclass java.util.EventObject and has a constructor that takes the UIComponent event source object as an argument. It also implements a type-safe accessor method for the event source object.

When a user clicks a button, it triggers an event represented by the javax.faces.event.ActionEvent class:

package javax.faces.event;

import javax.faces.component.UIComponent;

public class ActionEvent extends FacesEvent {
    public ActionEvent(UIComponent component) {
        super(component);
    }
    ...
}

Other events are represented by similar concrete subclasses, such as the javax.faces.event.ValueChangeEvent, which signals a value change.

Along ...

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.