The Java EE 7 Tutorial
15.6 Implementing an Event Listener
The JavaServer Faces technology supports action events and value-change events for components.
Action events occur when the user activates a component that implements javax.faces.component.ActionSource
. These events are represented by the class javax.faces.event.ActionEvent
.
Value-change events occur when the user changes the value of a component that implements javax.faces.component.EditableValueHolder
. These events are represented by the class javax.faces.event.ValueChangeEvent
.
One way to handle events is to implement the appropriate listener classes. Listener classes that handle the action events in an application must implement the interface javax.faces.event.ActionListener
. Similarly, listeners that handle the value-change events must implement the interface javax.faces.event.ValueChangeListener
.
This section explains how to implement the two listener classes.
To handle events generated by custom components, you must implement an event listener and an event handler and manually queue the event on the component. See Handling Events for Custom Components for more information.
Note: You do not need to create an |
15.6.1 Implementing Value-Change Listeners
A javax.faces.event.ValueChangeListener
implementation must include a processValueChange(ValueChangeEvent)
method. This method processes the specified value-change event and is invoked by the JavaServer Faces implementation when the value-change event occurs. The ValueChangeEvent
instance stores the old and the new values of the component that fired the event.
In the Duke's Bookstore case study, the NameChanged
listener implementation is registered on the name
UIInput
component on the bookcashier.xhtml
page. This listener stores into session scope the name the user entered in the field corresponding to the name component.
The bookreceipt.xhtml
subsequently retrieves the name from the session scope:
<h:outputFormat title="thanks" value="#{bundle.ThankYouParam}"> <f:param value="#{sessionScope.name}"/> </h:outputFormat>
When the bookreceipt.xhtml
page is loaded, it displays the name inside the message:
"Thank you, {0}, for purchasing your books from us."
Here is part of the NameChanged
listener implementation:
public class NameChanged extends Object implements ValueChangeListener { @Override public void processValueChange(ValueChangeEvent event) throws AbortProcessingException { if (null != event.getNewValue()) { FacesContext.getCurrentInstance().getExternalContext(). getSessionMap().put("name", event.getNewValue()); } } }
When the user enters the name in the field, a value-change event is generated, and the processValueChange(ValueChangeEvent)
method of the NameChanged
listener implementation is invoked. This method first gets the ID of the component that fired the event from the ValueChangeEvent
object, and it puts the value, along with an attribute name, into the session map of the FacesContext
instance.
Registering a Value-Change Listener on a Component explains how to register this listener onto a component.
15.6.2 Implementing Action Listeners
A javax.faces.event.ActionListener
implementation must include a processAction(ActionEvent)
method. The processAction(ActionEvent)
method processes the specified action event. The JavaServer Faces implementation invokes the processAction(ActionEvent)
method when the ActionEvent
occurs.
The Duke's Bookstore case study uses two ActionListener
implementations, LinkBookChangeListener
and MapBookChangeListener
. See Handling Events for Custom Components for details on MapBookChangeListener
.
Registering an Action Listener on a Component explains how to register this listener onto a component.