This topic covers the JavaServer Faces 1.1 Expression Language. You use this expression language with J2EE 1.4 and J2EE 1.3 projects, which use JavaServer Faces 1.1 components. If you are working with a Java EE 5 project, your components are JavaServer Faces 1.2 components, which use the Unified Expression Language. See the following web page for an article on that language:
For the full Unified Expression Language specification, you must download the JavaServer Pages (JSP) 2.1 specification. The following web page discusses JSP technology and provides links for downloads:
This topic is for advanced users who want to enter their own value binding expressions rather than letting the IDE create those expressions. It has the following sections:
JavaServer Faces provides an expression language (JSF EL) that is used in web application
pages to access the JavaBeans components in the page bean and in other beans associated with
the web application, such as the session bean and the application bean. The IDE in most cases
takes care of specifying the correct expression for you, for example, when you bind a
component's text
property to a data provider or to a JavaBean property.
To bind any property of a component, you can add the component to a Visual Web JSF page and then right-click the component and choose Property Bindings. You can then use the Property Bindings dialog box to select a property of the component and choose which JavaBeans property the component property is to be bound to.
As an example of binding a component to a database table, the following code sample references a Static Text component. Here's how to produce the code sample:
The IDE automatically adds a data provider object for that database table to the page
and binds the the text
property to the PERSON.PERSONID
field of
the data provider. You see the text of the component change to 123
.
PERSON.NAME
field of the data
provider and click OK to change the binding of the text
property to the
correct field.
The resulting code in the JSP editor looks like this:
<ui:staticText binding="#{Page1.staticText1}" id="staticText1" style="position: absolute; left: 216px; top: 192px" text="#{Page1.personDataProvider.value['PERSON.NAME']}"/>
staticText
. It uses the qualifier ui:
, which identifies the XML
namespace for the staticText
component. The ui:
qualifier is
defined in the page header as xmlns:ui="http://www.sun.com/web/ui"
. This
namespace points to a custom tag library for rendering UI components in the Basic,
Composite, and Layout categories of the Palette.
There are two other qualifiers that you will see in JSP code that are defined on this same line :
h:
- Defined in the page header as
xmlns:h="http://java.sun.com/jsf/html"
, this namespace points to a
JavaServer Faces custom tag library for rendering JavaServer Faces Reference
Implementation components that are primarily in the Standard category of the palette.
f:
- Defined in the page header as
xmlns:f="http://java.sun.com/jsf/core"
, this namespace points to a
JavaServer Faces custom tag library for representing event handlers, validators, and
other actions.
The TLD documentation for these two qualifiers is located at:
binding
attribute connects this component to a specific JavaBeans object
staticText1
in the Page1
page bean. The binding
attribute and the attributes id
, style
, and text
are
all JavaServer Faces tag library attributes. The last three attributes, id
,
style
, and text,
are represented in the IDE as properties of the
component and can be set in the component's
Properties window.
binding
and text
attributes use the JavaServer Faces
expression language. You can use the JavaServer Faces expression language to set the
value
attribute in the component's Properties window.
As described in the sections that follow, the JavaServer Faces expression language syntax
uses the delimiters #{}
. A JavaServer Faces expression can be a
value-binding expression (for binding UI components or their values to external data sources)
or a method-binding expression (for referencing backing bean methods). It can also accept
mixed literals and the evaluation syntax and operators of the 2.0 expression language.
JSF EL can be used to bind JavaBeans to component properties to simplify how the components
access data from various sources. JSF EL expressions use the syntax #{expr};
The syntax of a value binding expression is identical to the syntax of an expression language expression defined in the JavaServer Pages Specification (version 2.0), sections 2.3 through 2.9, with the following exceptions:
#{
and
}
instead${
and }
.
In addition to the differences in delimiters, the two expression types have the following semantic differences:
getValue
method) rather than by the compiled
code for a page.
VariableResolver
and PropertyResolver
objects available through
the Application
object for the current web application, for which applications
can provide plug-in replacement classes that provide additional capabilities.
EditableValueHolder
component (any input field component), the expression is
used to modify the referenced value rather than to retrieve it during the Update Model
Values phase of the request processing lifecycle.
Examples of valid value binding expressions include:
#{Page1.name} #{Foo.bar} #{Foo[bar]} #{Foo[“bar”]} #{Foo[3]} #{Foo[3].bar} #{Foo.bar[3]} #{Customer.status == ‘VIP’} #{(Page1.City.farenheitTemp - 32) * 5 / 9} Reporting Period: #{Report.fromDate} to #{Report.toDate}
For value binding expressions where the setValue
method is going to be called
(for example, for text
property bindings for input fields during Update Model
Values), the syntax of a value binding expression is limited to one of the following forms,
where expr-a
is a general expression that evaluates to some object, and
value-b
is an identifier:
#{expr-a.value-b} #{expr-a[value-b]] #{value-b}
When the getValue
method of a ValueBinding
instance is called (for
example, when an expression on a JSP tag attribute is being evaluated during the rendering of
the page), and the expression is evaluated, and the result of that evaluation is returned,
evaluation takes as follows:
.
and []
operators. expr-a.expr-b
is equivalent to a["expr-b"]
; that is,
the expression expr-b
is used to construct a literal whose value is the
identifier, and then the []
operator is used with that value.
VariableResolver
instance that is acquired from the Application instance for this web application. If the
value on the left side of the .
or []
operator is a
RowSet
, the object on the right side is treated as a column name. See the next
section for a more complete evaluation description of these operators.
.
or [...]
operators in an expression is
evaluated by the PropertyResolver
instance that is acquired from the
Application
instance for this web application.
Properties of variables are accessed by using the .
operator and can be
nested arbitrarily.
When the setValue
method of a ValueBinding
is called (for example,
for text
property bindings for input fields during Update Model Values), the
syntax of the value binding restriction is restricted as described in the previous section.
The implementation must perform the following processing to evaluate an expression of the
form #{expra.value-b}
or #{expr-a[value-b]}
:
expr-a
into value-a
.
value-a
is null, throw PropertyNotFoundException
.
value-b
is null, throw PropertyNotFoundException
.
value-a
is a Map, call value-a.put(value-b, new-value)
.
value-a
is a List
or an array:
value-b
to int
, throwing
ReferenceSyntaxException
on an error.
value-a.set(value-b, new-value)
or
Array.set(value-b, new-value)
as appropriate.
IndexOutOfBoundsException
or
ArrayIndexOutOfBoundsException
is thrown, throw
PropertyNotFoundException
.
EvaluationException
.
value-a
is a JavaBeans object):
value-b
to String
.
value-b
is a writeable property of value-a
(as per the
JavaBeans Specification), call the setter method (passing new-value
).
Throw ReferenceSyntaxException
if an exception is thrown.
PropertyNotFoundException
.
If the entire expression consists of a single identifier, the following rules apply:
ReferenceSyntaxException
.
new-value
.
new-value
.
The expression language defines a set of implicit objects:
facesContext
- The FacesContext instance for the current request.
param
- Maps a request parameter name to a single value.
paramValues
- Maps a request parameter name to an array of values.
header
- Maps a request header name to a single value.
headerValues
- Maps a request header name to an array of values.
cookie
- Maps a cookie name to a single cookie.
initParam
- Maps a context initialization parameter name to a single value.
Objects that allow access to various scoped variables:
requestScope
- Maps request-scoped variable names to their values.
sessionScope
- Maps session-scoped variable names to their values.
applicationScope
- Maps application-scoped variable names to their values.
When an expression references one of these objects by name, the appropriate object is
returned. An implicit object takes precedence over an attribute that has the same name. For
example, #{facesContext}
returns the FacesContext
object, even if
there is an existing facesContext
attribute containing some other value.
The expression language defines the following literals:
true
and false
"
is escaped as \"
, ' is
escaped as \
', and \
is escaped as \\
.
null
In addition to the .
and []
operators discussed above in Get Value Semantics and the section after that one, the expression
language provides the following operators:
+
, -
(binary), *
, /
,
div
, %
, mod
, -
(unary)
and
, &&
, or
, ||
,
not
, !
==
, eq
, !=
, ne
,
<
, lt
, >
, gt
, <=
,
ge
, >=
, le
. Comparisons can be made against other
values, or against boolean, string, integer, or floating point literals.
empty
operator is a prefix operation that can be used to determine
whether a value is null
or empty.
A ? B : C
. Evaluate B
or C
, depending
on the result of the evaluation of A
.
The precedence of operators highest to lowest, left to right is as follows:
[] .
()
(changes precedence of operators)
-
(unary) not ! empty
* / div % mod
+ -
(binary)
< > <= >= lt gt le ge
== != eq ne
&& and
|| or
? :
The following words are reserved for the expression language and must not be used as identifiers:
and
|
false
|
le
|
not
|
div
|
ge
|
lt
|
null
|
empty
|
gt
|
mod
|
or
|
eq
|
instanceof
|
ne
|
true
|