The Java EE 7 Tutorial
11.4 Referencing a Managed Bean Method
A component tag has a set of attributes for referencing managed bean methods that can perform certain functions for the component associated with the tag. These attributes are summarized in Table 11-6.
Table 11-6 Component Tag Attributes That Reference Managed Bean Methods
Attribute | Function |
---|---|
Refers to a managed bean method that performs navigation processing for the component and returns a logical outcome |
|
Refers to a managed bean method that handles action events |
|
Refers to a managed bean method that performs validation on the component's value |
|
Refers to a managed bean method that handles value-change events |
Only components that implement ActionSource
can use the action
and actionListener
attributes. Only components that implement EditableValueHolder
can use the validator
or valueChangeListener
attributes.
The component tag refers to a managed 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 documentation at http://docs.oracle.com/javaee/7/javaserverfaces/2.2/vdldocs/facelets/
. For example, the definition of the validator
attribute of the inputText
tag is the following:
void validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
The following sections give examples of how to use the attributes.
11.4.1 Referencing a Method That Performs Navigation
If your page includes a component, such as a button or a link, 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 following:
-
Specifies a logical outcome
String
that tells the application which page to access next -
References a managed bean method that performs some processing and returns a logical outcome
String
The following example shows how to reference a navigation method:
<h:commandButton value="#{bundle.Submit}" action="#{cashierBean.submit}" />
See Writing a Method to Handle Navigation for information on how to write such a method.
11.4.2 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 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 link rendered by this component. See Writing a Method to Handle an Action Event for information on how to write such a method.
11.4.3 Referencing a Method That Performs Validation
If the input of one of the components on your page is validated by a managed bean method, refer to the method from the component's tag by using the validator
attribute.
The following simplified example from The guessnumber-cdi CDI Example shows how to reference a method that performs validation on inputGuess
, an input component:
<h:inputText id="inputGuess" value="#{userNumberBean.userNumber}" required="true" size="3" disabled="#{userNumberBean.number eq userNumberBean.userNumber ...}" validator="#{userNumberBean.validateNumberRange}"> </h:inputText>
The managed bean method validateNumberRange
verifies that the input value is within the valid range, which changes each time another guess is made. See Writing a Method to Perform Validation for information on how to write such a method.
11.4.4 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 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="#{cashierBean.name}" required="true" valueChangeListener="#{cashierBean.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.
Writing a Method to Handle a Value-Change Event describes how to implement a method that handles a ValueChangeEvent
.