JavaServer Faces Expression Language

See Also

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:

Introduction
JavaServer Faces EL Expression Syntax
Get Value Semantics
Set Value Semantics
Implicit Objects
Literals
Operators
Reserved Words

Introduction

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:

  1. Drag the Static Text component output text icon from the Basic category of the Palette to a Visual Web JSF page in the Visual Designer.
  2. Open the and drag the Person table from the Travel database and drop it on the component.

    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.

  3. Right-click the component and choose Bind to Data.
  4. In the Bind to Data dialog box, choose the PERSON.NAME field of the data provider and click OK to change the binding of the text property to the correct field.
  5. Click the JSP button above the page to see the resulting source code.

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']}"/>

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.

JavaServer Faces EL Expression Syntax

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}

Get Value Semantics

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:

Set Value Semantics

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:

Implicit Objects

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.

Literals

The expression language defines the following literals:

Operators

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:

Reserved Words

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
See Also
Adding Components to a Visual Web JSF Page
About Binding Components to Data
Binding Component Properties
About Pages

Legal Notices