2.1.6 Events and listeners
"JSF leverages JavaBeans to handle events with event objects and listeners, just like Swing. Any component may fire zero or more events, and developers (or components themselves) can register zero or more listeners to handle those events." Encouraging developers to think in terms of events is meant to free them from thinking about HTTP requests and responses.
"There are four standard events: value-change events, action events, data model events, and phase events." Phase events "execute while JSF processes an HTTP request."
Value-change events
A sample value-change event declaration:
valueChangeListener="#{myForm.processValueChanged}"
The corresponding, backing-bean stub:
public void processValueChanged(ValueChangeEvent event)
{
//...
}
Action events
Components that generate action events are also called "action sources." Two types of "action listeners" handle action events: "those that affect navigation [wired to an action method], and those that don't [wired to an action listener method]." Those that don't cause the page firing the event to be redisplayed.
"Technically all navigation is handled by a single action listener. This listener automatically handles any action events fired by the component, so it doesn't need to be registered manually. By default, this action listener delegates all of its work to action methods in your backing beans... most of your application logic will be located in these methods."
A sample action event declaration:
action="#{loginForm.login}"
The corresponding, backing-bean method:
public class LoginForm
{
public String login() { return "success"; }
}
The login() method returns a "dynamic outcome" a "static outcome" (with the same affect as the above looks like this:
action="success"
A sample action listener event declaration:
actionListener="#{myForm.doIt}"
The corresponding, backing-bean method stub:
public void doIt(ActionEvent event)
{
//...
}
"Usually, you use action listener methods for changes that affect the current view." Note that action listener methods do not return values (but accept one parameter of type ActionEvent), while action methods take no parameters but require a String return value.
Data model events
"Data model events are a little different than the other events because they're not actually fired by the UI component... you can't register a listener on the component itself in JSP. You have to register it in Java code instead..." You must implement an interface to define a data model event.
"Since data model events are fired so many times, they're normally used when developing a data-driven component, rather than during application development." It seems like the author is saying that data model events are used at design time.
Phase events
"Whenever a JSF application receives a request, it goes through a six-step process called the Request Processing Lifecycle": (i) getting the view based on the request, (ii) obtaining component values from request parameters, (iii) validating input, (iv) updating backing beans and model objects, (v) invoking action listeners and (vi) returning a response.
Each of these six "phases" has two associated phase events. "Phase events are generated by JSF itself rather than by UI components, and require that you implement a Java interface to register event listeners. They're normally used internally by the JSF implementation..."