background image

Using Functions

<< Examples of EL Expressions | Defining Functions >>
<< Examples of EL Expressions | Defining Functions >>

Using Functions

TABLE 5­5
Example Expressions
(Continued)
EL Expression
Result
${departments[deptName]}
The value of the entry named deptName in the departments
map.
${requestScope[
'javax.servlet.forward.
servlet_path
']}
The value of the request-scoped attribute named
javax.servlet.forward.servlet_path
.
#{customer.lName}
Gets the value of the property lName from the customer bean
during an initial request. Sets the value of lName during a
postback.
#{customer.calcTotal}
The return value of the method calcTotal of the customer
bean.
Functions
The JSP expression language allows you to define a function that can be invoked in an
expression. Functions are defined using the same mechanisms as custom tags (see
"Using
Custom Tags" on page 172
and
Chapter 8, "Custom Tags in JSP Pages"
).
At first glance, functions seem similar to method expressions, but they are different in the
following ways:
Functions refer to static methods that return a value. Method expressions refer to non-static,
arbitrary public methods on objects.
Functions are identified statically at translation time, whereas methods are identified
dynamically at runtime.
Function parameters and invocations are specified as part of an EL expression. A method
expression only identifies a particular method. The invocation of that method is not
specified by the EL expression; rather, it is specified in the tag attribute definition of the
attribute using the method expression, as described in
"Defining a Tag Attribute Type" on
page 156
.
Using Functions
Functions can appear in static text and tag attribute values.
To use a function in a JSP page, you use a taglib directive to import the tag library containing
the function. Then you preface the function invocation with the prefix declared in the directive.
For example, the date example page index.jsp imports the /functions library and invokes the
function equals in an expression:
<%@ taglib prefix=
"f" uri="/functions"%>
...
Unified Expression Language
Chapter 5 · JavaServer Pages Technology
165