type=page
status=published
title=Implementing an Event Listener
next=jsf-custom008.html
prev=jsf-custom006.html
~~~~~~
Implementing an Event Listener
==============================

[[BNAUT]]

[[implementing-an-event-listener]]
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 link:jsf-custom008.html#BNAWD[Handling Events for Custom
Components] for more information.


[width="100%",cols="100%",]
|=======================================================================
a|
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. See
link:jsf-develop003.html#BNAVC[Writing a Method to Handle Navigation] for
information on how to manage page navigation.

|=======================================================================


[[BNAUU]]

[[implementing-value-change-listeners]]
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:

[source,oac_no_warn]
----
<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:

[source,oac_no_warn]
----
"Thank you, {0}, for purchasing your books from us."
----

Here is part of the `NameChanged` listener implementation:

[source,oac_no_warn]
----
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.

link:jsf-page-core002.html#BNATA[Registering a Value-Change Listener on a
Component] explains how to register this listener onto a component.

[[BNAUV]]

[[implementing-action-listeners]]
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 link:jsf-custom008.html#BNAWD[Handling Events for Custom Components]
for details on `MapBookChangeListener`.

link:jsf-page-core002.html#BNATB[Registering an Action Listener on a
Component] explains how to register this listener onto a component.


