Referencing a Backing Bean Method
A component tag has a set of attributes for referencing backing bean methods that can perform certain functions for the component associated with the tag. These attributes are summarized in Table 10-10.
Only components that implement
ActionSource
can use theaction
andactionListener
attributes. Only components that implementEditableValueHolder
can use thevalidator
orvalueChangeListener
attributes.The component tag refers to a backing bean method using a method expression as a value of one of the attributes. The method referenced by an attribute must follow a particular signature, which is defined by the tag attribute's definition in the TLD. For example, the definition of the
validator
attribute of theinputText
tag inhtml_basic.tld
is the following:void validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)The following four sections give examples of how to use the four different attributes.
Referencing a Method That Performs Navigation
If your page includes a component (such as a button or hyperlink) that causes the application to navigate to another page when the component is activated, the tag corresponding to this component must include an
action
attribute. This attribute does one of the followingThe
bookcashier.jsp
page of the Duke's Bookstore application has acommandButton
tag that refers to a backing bean method that calculates the shipping date. If the customer has ordered more than $100 (or 100 euros) worth of books, this method also sets therendered
properties of some of the components totrue
and returnsnull
; otherwise it returnsreceipt
, which causes thebookreceipt.jsp
page to display. Here is thecommandButton
tag from thebookcashier.jsp
page:The
action
attribute uses a method expression to refer to thesubmit
method ofCashierBean
. This method will process the event fired by the component corresponding to this tag.Writing a Method to Handle Navigation (page 407) describes how to implement the
submit
method ofCashierBean
.The application architect must configure a navigation rule that determines which page to access given the current page and the logical outcome, which is either returned from the backing bean method or specified in the tag. See Configuring Navigation Rules (page 463) for information on how to define navigation rules in the application configuration resource file.
Referencing a Method That Handles an Action Event
If a component on your page generates an action event, and if that event is handled by a backing bean method, you refer to the method by using the component's
actionListener
attribute.The
chooselocale.jsp
page of the Duke's Bookstore application includes some components that generate action events. One of them is theNAmerica
component:<h:commandLink id="NAmerica" action="bookstore" actionListener="#{localeBean.chooseLocaleFromLink}">The
actionListener
attribute of this component tag references thechooseLocaleFromLink
method using a method expression. ThechooseLocaleFromLink
method handles the event of a user clicking on the hyperlink rendered by this component.Writing a Method to Handle an Action Event (page 409) describes how to implement a method that handles an action event.
Referencing a Method That Performs Validation
If the input of one of the components on your page is validated by a backing bean method, you refer to the method from the component's tag using the
validator
attribute.The Coffee Break application includes a method that performs validation of the
checkoutForm.jsp
page. Here is the tag corresponding to this component:<h:inputText id="email" value="#{checkoutFormBean.email}" size="25" maxlength="125" validator="#{checkoutFormBean.validateEmail}"/>This tag references the
validate
method described in Writing a Method to Perform Validation (page 409) using a method expression.Writing a Method to Perform Validation (page 409) describes how to implement a method that performs validation.
Referencing a Method That Handles 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 backing bean method, you refer to the method using the component's
valueChangeListener
attribute.The
name
component on thebookcashier.jsp
page of the Duke's Bookstore application references aValueChangeListener
implementation that handles the event of a user entering a name in thename
input field:<h:inputText id="name" size="50" value="#{cashier.name}" required="true"> <f:valueChangeListener type="listeners.NameChanged" /> </h:inputText>For illustration, Writing a Method to Handle a Value-Change Event (page 410) describes how to implement this listener with a backing bean method instead of a listener implementation class. To refer to this backing bean method, the tag uses the
valueChangeListener
attribute:<h:inputText id="name" size="50" value="#{cashier.name}" required="true" valueChangeListener="#{cashier.processValueChange}" /> </h:inputText>The
valueChangeListener
attribute of this component tag references theprocessValueChange
method ofCashierBean
using a method expression. TheprocessValueChange
method handles the event of a user entering his name in the input field rendered by this component.Writing a Method to Handle a Value-Change Event (page 410) describes how to implement a method that handles a
ValueChangeEvent
.