background image

Method Expressions

<< Seting Tag Attribute Values | Defining aTag AttributeType >>
<< Seting Tag Attribute Values | Defining aTag AttributeType >>

Method Expressions

return a result. A similar feature of the unified EL is functions. Method expressions differ from
functions in many ways.
"Functions" on page 165
explains more about the differences between
functions and method expressions.
Method expressions primarily benefit JavaServer Faces technology, but they are available to any
technology that can support the unified expression language. Let's take a look at how JavaServer
Faces technology employs method expressions.
In JavaServer Faces technology, a component tag represents a UI component on a page. The
component tag uses method expressions to invoke methods that perform some processing for
the component. These methods are necessary for handling events that the components generate
and validating component data, as shown in this example:
<h:form>
<h:inputText
id=
"name"
value=
"#{customer.name}"
validator=
"#{customer.validateName}"/>
<h:commandButton
id=
"submit"
action=
"#{customer.submit}" />
</h:form>
The inputText tag displays a UIInput component as a text field. The validator attribute of
this inputText tag references a method, called validateName, in the bean, called customer. The
TLD (see
"Tag Library Descriptors" on page 247
) that defines the inputText tag specifies what
signature the method referred to by the validator attribute must have. The same is true of the
customer.submit
method referenced by the action attribute of the commandButton tag. The
TLD specifies that the submit method must return an Object instance that specifies which page
to navigate to next after the button represented by the commandButton tag is clicked.
The validation method is invoked during the process validation phase of the life cycle,
whereas the submit method is invoked during the invoke application phase of the life cycle.
Because a method can be invoked during different phases of the life cycle, method expressions
must always use the deferred evaluation syntax.
Similarly to lvalue expressions, method expressions can use the . and [] operators. For example,
#{object.method}
is equivalent to #{object["method"]}. The literal inside the [] is coerced to
String
and is used to find the name of the method that matches it. Once the method is found, it
is invoked or information about the method is returned.
Unified Expression Language
Chapter 5 · JavaServer Pages Technology
155