Registering Listeners on Components
A page author can register a listener implementation class on a component by nesting either a
valuechangeListener
tag or anactionListener
tag within the component's tag on the page.An application developer can instead implement these listeners as backing bean methods. To reference these methods, a page author uses the component tag's
valueChangeListener
andactionListener
attributes, as described in Referencing a Method That Handles an Action Event and Referencing a Method That Handles a Value-change Event.The Duke's Bookstore application includes a
ValueChangeListener
implementation class but does not use anActionListener
implementation class. This section explains how to register theNameChanged
value-change listener and a hypotheticalLocaleChange
action listener implementation on components. Implementing Value-Change Listeners explains how to implementNameChanged
. Implementing Action Listeners explains how to implement the hypotheticalLocaleChange
.Registering a Value-Change Listener on a Component
A page author can register a
ValueChangeListener
implementation on aUIInput
component or a component represented by one of the subclasses ofUIInput
by nesting avalueChangeListener
tag within the component's tag on the page. Here is the tag corresponding to thename
component from thebookcashier.jsp
page:<h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText>The
type
attribute of thevalueChangeListener
tag specifies the fully qualified class name of theValueChangeListener
implementation.After this component tag is processed and local values have been validated, its corresponding component instance will queue the
ValueChangeEvent
associated with the specifiedValueChangeListener
to the component.Registering an Action Listener on a Component
A page author can register an
ActionListener
implementation on aUICommand
component by nesting anactionListener
tag within the component's tag on the page. Duke's Bookstore does not use anyActionListener
implementations. Here is one of thecommandLink
tags on thechooselocale.jsp
page, changed to reference anActionListener
implementation rather than a backing bean method:<h:commandLink id="NAmerica" action="bookstore"> <f:actionListener type="listeners.LocaleChange" /> </h:commandLink>The
type
attribute of theactionListener
tag specifies the fully qualified class name of theActionListener
implementation.When this tag's component is activated, the component's
decode
method (or its associatedRenderer
) automatically queues theActionEvent
implementation associated with the specifiedActionListener
implementation onto the component.