type=page
status=published
title=Referencing a Managed Bean Method
next=jsf-develop.html
prev=jsf-page-core003.html
~~~~~~
= Referencing a Managed Bean Method


[[BNATN]]

[[referencing-a-managed-bean-method]]
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 link:#BNATO[Table
11-7].

[[sthref66]][[BNATO]]

*Table 11-7 Component Tag Attributes That Reference Managed Bean Methods*

[width="60%",cols="10%,50%"]
|=======================================================================
|*Attribute* |*Function*
|`action` |Refers to a managed bean method that performs navigation
processing for the component and returns a logical outcome `String`

|`actionListener` |Refers to a managed bean method that handles action
events

|`validator` |Refers to a managed bean method that performs validation
on the component's value

|`valueChangeListener` |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 olink:JSFTL[JavaServer Faces Facelets
Tag Library documentation]. For example, the definition of the
`validator` attribute of the `inputText` tag is the following:

[source,oac_no_warn]
----
void validate(javax.faces.context.FacesContext,
              javax.faces.component.UIComponent, java.lang.Object)
----

The following sections give examples of how to use the attributes.

[[BNATP]]

[[referencing-a-method-that-performs-navigation]]
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:

[source,oac_no_warn]
----
<h:commandButton value="#{bundle.Submit}"
                 action="#{cashierBean.submit}" />
----

See link:jsf-develop003.html#BNAVC[Writing a Method to Handle Navigation]
for information on how to write such a method.

[[BNATQ]]

[[referencing-a-method-that-handles-an-action-event]]
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:

[source,oac_no_warn]
----
<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
link:jsf-develop003.html#BNAVD[Writing a Method to Handle an Action
Event] for information on how to write such a method.

[[BNATR]]

[[referencing-a-method-that-performs-validation]]
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
link:cdi-basicexamples003.html#GJCXV[The guessnumber-cdi CDI Example]
shows how to reference a method that performs validation on
`inputGuess`, an input component:

[source,oac_no_warn]
----
<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 link:jsf-develop003.html#BNAVE[Writing a Method to Perform
Validation] for information on how to write such a method.

[[BNATS]]

[[referencing-a-method-that-handles-a-value-change-event]]
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:

[source,oac_no_warn]
----
<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.

link:jsf-develop003.html#BNAVF[Writing a Method to Handle a Value-Change
Event] describes how to implement a method that handles a
`ValueChangeEvent`.
