Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Registering Listeners on Components

A page author can register a listener implementation class on a component by nesting either a valuechangeListener tag or an actionListener 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 and actionListener attributes, as described in Referencing a Method That Handles an ActionEvent (page 721) and Referencing a Method That Handles a ValueChangeEvent (page 722).

The Duke's Bookstore application includes a value-change listener implementation class but does not use an action listener implementation class. This section explains how to register the NameChanged ValueChangeListener and a hypothetical LocaleChange ActionListener implementation on components. Implementing Value-Change Listeners (page 748) explains how to implement NameChanged. Implementing Action Listeners (page 749) explains how to implement the hypothetical LocaleChange.

Registering a ValueChangeListener on a Component

A page author can register a ValueChangeListener implementation on a UIInput component or a component represented by one of the subclasses of UIInput by nesting a valueChangeListener tag within the component's tag on the page. Here is the tag corresponding to the name component from the bookcashier.jsp page:

<h:inputText  id="name" size="50" value="#{cashier.name}"
  required="true">  
  <f:valueChangeListener type="listeners.NameChanged" />
</h:inputText>

The type attribute of the valueChangeListener tag specifies the fully qualified class name of the ValueChangeListener implementation.

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.

Registering an ActionListener 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. 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.

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 to the component.

  • + Share This
  • 🔖 Save To Your Account