JSF - Listener

> Procedural Languages > Java > JavaServer Faces (JSF)

1 - About

Listeners are used to listen to the events happening in the page and perform actions as defined.

An application developer can implement listeners as:

Advertising

3 - Referencing a Method That Handles

3.1 - an Action Event

If a component on your page generates an action event, and if that event is handled by a managed bean method, you refer to the method by using the component’s actionListener attribute.

The following example shows how such a method could be referenced:

<h:commandLink id="Duke" action="bookstore"
     actionListener="#{actionBean.chooseBookFromLink}">

The actionListener attribute of this component tag references the chooseBookFromLink method using a method expression. The chooseBookFromLink method handles the event when the user clicks the hyperlink rendered by this component.

3.2 - a Value-Change Event

If you want a component on your page to generate a value-change event and you want that event to be handled by a managed bean method instead of a ValueChangeListener implementation, you refer to the method by using the component’s valueChangeListener attribute:

<h:inputText id="name"
             size="30"
             value="#{cashier.name}"
             required="true"
             valueChangeListener="#{cashier.processValueChange}" />
</h:inputText>

The valueChangeListener attribute of this component tag references the processValueChange method of CashierBean by using a method expression. The processValueChange method handles the event of a user entering a name in the input field rendered by this component.

4 - Documentation / Reference

Advertising