background image

Registering an Action Listener

<< Registering Listeners on Components | Using the Standard Validators >>
<< Registering Listeners on Components | Using the Standard Validators >>

Registering an Action Listener

<h:inputText
id=
"name" size="50" value="#{cashier.name}"
required=
"true">
<f:valueChangeListener type=
"listeners.NameChanged" />
</h:inputText>
The type attribute specifies the custom NameChanged listener as the ValueChangeListener
implementation to register on the name component.
After this component tag is processed and local values have been validated, its corresponding
component instance will queue the ValueChangeEvent associated with the specified
ValueChangeListener
to the component.
The binding attribute is used to bind a ValueChangeListener implementation to a backing
bean property. It works in a similar way to the binding attribute supported by the standard
converter tags.
"Binding Component Values and Instances to External Data Sources" on
page 364
explains more about binding listeners to backing bean properties.
Registering an Action Listener on a Component
A page author can register an ActionListener implementation on a UICommand component by
nesting an actionListener tag within the component's tag on the page. Similarly to the
valueChangeListener
tag, the actionListener tag supports both the type and binding
attributes. A page author must use one of these attributes to reference the action listener.
Duke's Bookstore does not use any ActionListener implementations. Here is one of the
commandLink
tags on the chooselocale.jsp page, changed to reference an ActionListener
implementation rather than a backing bean method:
<h:commandLink id=
"NAmerica" action="bookstore">
<f:actionListener type=
"listeners.LocaleChange" />
</h:commandLink>
The type attribute of the actionListener tag specifies the fully qualified class name of the
ActionListener
implementation. Similarly to the valueChangeListener tag, the
actionListener
tag also supports the binding attribute.
"Binding Converters, Listeners, and
Validators to Backing Bean Properties" on page 369
explains more about how to bind listeners
to backing bean properties.
When this tag's component is activated, the component's decode method (or its associated
Renderer
) automatically queues the ActionEvent implementation associated with the specified
ActionListener
implementation onto the component.
In addition to the actionListener tag that allows you register a custom listener onto a
component, the core tag library includes the setPropertyActionListener tag. You use this tag
to register a special action listener onto the ActionSource instance associated with a
component. When the component is activated, the listener will store the object referenced by
the tag's value attribute into the object referenced by the tag's target attribute.
Registering Listeners on Components
The Java EE 5 Tutorial · September 2007
360