JavaServer Faces Expression Language |
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 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:
text property to the CUSTOMER_TABLE.CUSTOMER_NUM field of the data provider. You see the text of the component change to 123.
CUSTOMER_TABLE.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.customer_tblDataProvider.value['CUSTOMER_TBL.NAME']}"/>
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.
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:
http://java.sun.com/j2ee/javaserverfaces/1.1/docs/tlddocs/index.html.
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:
In addition to the differences in delimiters, the two expression types have the following semantic differences:
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:
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]}:
If the entire expression consists of a single identifier, the following rules apply:
The expression language defines a set of implicit objects:
Objects that allow access to various scoped variables:
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:
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:
The precedence of operators highest to lowest, left to right is as follows:
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 |
| Legal Notices. Copyright © 2006 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. |