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 with the event classes, there are listener interfaces declaring the methods that the event source calls to notify listeners of the event. A listener interface can contain methods for many related events, but for the JSF component events, there's a separate interface per event. Here's the javax.faces.event.ActionListener
interface:
package javax.faces.event;
import javax.faces.component.UIComponent;
public interface ActionListener extends FacesListener {
public void processAction(ActionEvent event)
throws AbortProcessingException;
}
When a component notices that a user event has happened, it creates an instance of the corresponding event class and adds it to an event list. Eventually, JSF tells the component to fire the event, i.e., loop through the list of listeners for that event and call the event notification method on each one.