Implementing an Event Listener (original) (raw)
The JavaServer Faces technology supports action events and value-change events for components.
Action events occur when the user activates a component that implementsjavax.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 classjavax.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 interfacejavax.faces.event.ActionListener
. Similarly, listeners that handle the value-change events must implement the interfacejavax.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 ActionListener implementation to handle an event that results solely in navigating to a page and does not perform any other application-specific processing. SeeWriting a Method to Handle Navigation for information on how to manage page navigation. |
---|
Implementing Value-Change Listeners
A javax.faces.event.ValueChangeListener
implementation must include aprocessValueChange(ValueChangeEvent)
method. This method processes the specified value-change event and is invoked by the JavaServer Faces implementation when the value-change event occurs. TheValueChangeEvent
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 thebookcashier.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 theNameChanged
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.
Implementing Action Listeners
A javax.faces.event.ActionListener
implementation must include aprocessAction(ActionEvent)
method. The processAction(ActionEvent)
method processes the specified action event. The JavaServer Faces implementation invokes the processAction(ActionEvent)
method when theActionEvent
occurs.
The Duke’s Bookstore case study uses two ActionListener
implementations, LinkBookChangeListener
and MapBookChangeListener
. See Handling Events for Custom Componentsfor details on MapBookChangeListener
.