Unified Expression Language

The primary new feature of JSP 2.1 is the unified expression language (unified EL), which represents a union of the expression language offered by JSP 2.0 and the expression language created for JavaServer Faces technology (see Chapter 9) version 1.0.

The expression language introduced in JSP 2.0 allows page authors to use simple expressions to dynamically read data from JavaBeans components. For example, the test attribute of the following conditional tag is supplied with an EL expression that compares the number of items in the session-scoped bean named cart with 0.

<c:if test="${sessionScope.cart.numberOfItems > 0}">  
  ... 
</c:if> 

As explained in The Life Cycle of a JSP Page, JSP supports a simple request/response life cycle, during which a page is executed and the HTML markup is rendered immediately. Therefore, the simple, read-only expression language offered by JSP 2.0 was well suited to the needs of JSP applications.

JavaServer Faces technology, on the other hand, features a multi-phase life cycle designed to support its sophisticated UI component model. In order to convert and validate component data, propagate component data to objects, and handle component events, JavaServer Faces technology introduced its own expression language that could allow deferred evaluation of expressions, the ability to set data as well as get data, and the ability to invoke methods. See Using the Unified EL to Reference Backing Beans for more information on how to use the unified EL in JavaServer Faces applications.

These two expression languages have been unified for a couple reasons. One reason is so that page authors can mix JSP content with JavaServer Faces tags without worrying about conflicts caused by the different life cycles these technologies support. Another reason is so that other JSP-based technologies could make use of the additional features similarly to the way JavaServer Faces technology uses them. In fact, although the standard JSP tags and static content continue to use only those features present in JSP 2.0, authors of JSP custom tags can create tags that take advantage of the new set of features in the unified expression language.

To summarize, the new, unified expression language allows page authors to use simple expressions to perform the following tasks:

The unified EL also allows custom tag developers to specify which of the following kinds of expressions that a custom tag attribute will accept:

Finally, the unified EL also provides a pluggable API for resolving expressions so that application developers can implement their own resolvers that can handle expressions not already supported by the unified EL.

This section gives an overview of the unified expression language features by explaining the following topics:

Immediate and Deferred Evaluation Syntax

One key feature of the unified EL is its support for both immediate and deferred evaluation of expressions. Immediate evaluation means that the JSP engine evaluates the expression and returns the result immediately when the page is first rendered. Deferred evaluation means that the technology using the expression language can employ its own machinery to evaluate the expression sometime later during the page's life cycle, whenever it is appropriate to do so. Those expressions that are evaluated immediately use the ${} syntax, which was introduced with the JSP 2.0 expression language. Expressions whose evaluation is deferred use the #{} syntax, which was introduced with by JavaServer Faces technology.

Because of its multi-phase life cycle, JavaServer Faces technology uses deferred evaluation expressions. During the life cycle, component events are handled, data is validated, and other tasks are performed, all done in a particular order. Therefore, it must defer evaluation of expressions until the appropriate point in the life cycle.

Other technologies using the unified EL might have different reasons for using deferred expressions.

Immediate Evaluation

All expressions using the ${} syntax are evaluated immediately. These expressions can only be used within template text or as the value of a JSP tag attribute that can accept runtime expressions.

The following example shows a tag whose value attribute references an immediate evaluation expression that gets the total price from the session-scoped bean named cart:

<fmt:formatNumber value="${sessionScope.cart.total}"/> 

The JSP engine evaluates the expression, ${sessionScope.cart.total}, converts it, and passes the returned value to the tag handler.

Immediate evaluation expressions are always read-only value expressions. The expression shown above can only get the total price from the cart bean; it cannot set the total price.

Deferred Evaluation

Deferred evaluation expressions take the form #{expr} and can be evaluated at other phases of a page life cycle as defined by whatever technology is using the expression. In the case of JavaServer Faces technology, its controller can evaluate the expression at different phases of the life cycle depending on how the expression is being used in the page.

The following example shows a JavaServer Faces inputText tag, which represents a text field component into which a user enters a value. The inputText tag's value attribute references a deferred evaluation expression that points to the name property of the customer bean.

<h:inputText id="name" value="#{customer.name}" /> 

For an initial request of the page containing this tag, the JavaServer Faces implementation evaluates the #{customer.name} expression during the render response phase of the life cycle. During this phase, the expression merely accesses the value of name from the customer bean, as is done in immediate evaluation.

For a postback, the JavaServer Faces implementation evaluates the expression during apply request values, process validations, and update model phases, during which the value is retrieved from the request, validated, and propagated to the customer bean.

As shown in this example, deferred evaluation expressions can be value expressions that can be used to both read and write data. They can also be method expressions. Value expressions (both immediate and deferred) and method expressions are explained in the next section.

Value and Method Expressions

The unified EL defines two kinds of expressions: value expressions and method expressions. Value expressions can either yield a value or set a value. Method expressions reference methods that can be invoked and can return a value.

Value Expressions

Value expressions can be further categorized into rvalue and lvalue expressions. Rvalue expressions are those that can read data, but cannot write it. Lvalue expressions can both read and write data.

All expressions that are evaluated immediately use the ${} delimiters and are always rvalue expressions. Expressions whose evaluation can be deferred use the #{} delimiters and can act as both rvalue and lvalue expressions. Consider these two value expressions:

<taglib:tag value="${customer.name}" /> 
<taglib:tag value="#{customer.name}" /> 

The former uses immediate evaluation syntax, whereas the latter uses deferred evaluation syntax. The first expression accesses the name property, gets its value, and the value is added to the response and rendered on the page. The same thing can happen with the second expression. However, the tag handler can defer the evaluation of this expression to a later time in the page life cycle, if the technology using this tag allows it.

In the case of JavaServer Faces technology, the latter tag's expression is evaluated immediately during an initial request for the page. In this case, this expression acts as an rvalue expression. During a postback, this expression can be used to set the value of the name property with user input. In this situation, the expression acts as an lvalue expression.

Syntax of Value Expressions

Both rvalue and lvalue expressions can refer to JavaBeans components, collections, and implicit objects. See Implicit Objects for more detail on the implicit objects available with JSP technology.

To refer to these objects, you write an expression using the name with which you created the object, as shown here:

${customer} 

See Creating and Using a JavaBeans Component for information on how to declare a JavaBeans component for use in your JSP page. See Backing Beans for information on how to declare beans for use in JSF applications.

To refer to properties of a bean, items of a collection, or attributes of an implicit object, you use the . or [] notation, which is similar to the notation used by ECMAScript.

So, instead of using ${customer.lName}, you can use the expression ${customer["lName"]} in which the part inside the square brackets is a string literal that is the name of the property to reference.

You can use double or single quotes for the String literal. You can also combine the [] and . notations, as shown here:

${customer.address["street"]} 

If you are accessing an item in an array or list, you must use either a literal value that can be coerced to int or the [] notation with an int and without quotes. The following examples could all resolve to the same item in a list or array, assuming that socks can be coerced to int:

In contrast, an item in a Map can be accessed using a string literal key; no coercion is required:

${customer.orders["socks"]} 

An rvalue expression can further directly refer to values that are not objects, such as the result of arithmetic operations and literal values, as shown by these examples:

The unified expression language defines the following literals:

Where Value Expressions Can Be Used

Value expressions using the ${} delimiters can be used in the following places:

The value of an expression in static text is computed and inserted into the current output. Here is an example of an expression embedded in static text:

<some:tag>
  some text ${expr} some text
</some:tag> 

If the static text appears in a tag body, note that an expression will not be evaluated if the body is declared to be tagdependent (see body-content Attribute).

LValue expressions can only be used in tag attributes that can accept lvalue expressions.

There are three ways to set a tag attribute value using either an rvalue or lvalue expression:

Expressions used to set attribute values are evaluated in the context of an expected type. If the result of the expression evaluation does not match the expected type exactly, a type conversion will be performed. For example, the expression ${1.2E4} provided as the value of an attribute of type float will result in the following conversion:

Float.valueOf("1.2E4").floatValue()  

See section 1.17 of the Expression Language specification for the complete type conversion rules.

Method Expressions

Another feature of the unified expression language is its support of deferred method expressions. A method expression is used to invoke an arbitrary public method, which can return a result. A similar feature of the unified EL is functions. Method expressions differ from functions in many ways. Functions 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 deploys a UIInput component as a text field. The validator attribute of the inputText tag references a method, called validateName, in the bean, called customer. The TLD 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 a string that specifies with 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 String literal inside the [] is coerced to String and used to find the name of the method that matches the String. Once the method is found, it is invoked or information about the method is returned.

Method expressions can be used only in tag attributes and only in the following ways:

Defining a Tag Attribute Type

As explained in the previous section, all kinds of expressions can be used in tag attributes. Which kind of expression and how that expression is evaluated--whether immediately or deferred--is determined by the type attribute of the tag's definition in the TLD file that defines the tag.

If you plan to create custom tags (see the chapter Custom Tags), you need to specify for each tag in the TLD what kind of expression it accepts. Table 4-2 shows the three different kinds of tag attributes that accept EL expressions, gives examples of expressions they accept and the type definitions of the attributes that must be added to the TLD. Note that it is illegal to use #{} syntax for a dynamic attribute or ${} syntax for a deferred attribute.

Table 4-2 Definitions of Tag Attributes That Accept EL Expressions
attribute type
example expression
type attribute definition
dynamic
"literal"
<rtexprvalue>true</rtexprvalue>
${literal}
<rtexprvalue>true</rtexprvalue>
deferred value
"literal"
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
#{customer.age}
<deferred-value>
<type>int</type>
</deferred-value>
deferred method
"literal"
<deferred-method>
<method-signature>
java.lang.String submit()
</method-signature>
<deferred-method>
#{customer.calcTotal}
<deferred-method>
<method-signature>
double calcTotal(int, double)
</method-signature>
</deferred-method

In addition to the tag attribute types shown in Table 4-2, you can also define an attribute to accept both dynamic and deferred expressions. In this case, the tag attribute definition contains both an rtexprvalue definition set to true and either a deferred-value or deferred-method definition.

Deactivating Expression Evaluation

Because the patterns that identify EL expressions--${ } and #{ }--were not reserved in the JSP specifications before JSP 2.0, there may be applications where such patterns are intended to pass through verbatim. To prevent the patterns from being evaluated, you can deactivate EL evaluation in one of the following ways:

Literal Expressions

A literal expression evaluates to the text of the expression, which is of type String. It does not use the ${} or #{} delimiters.

If you have a literal expression that includes the reserved ${} or #{} syntax, you need to escape these characters as follows.

When a literal expression is evaluated, it can be converted to another type. Table 4-3 shows examples of various literal expressions and their expected types and resulting values.

Table 4-3 Literal Expressions
Expression
Expected Type
Result
Hi
String
Hi
true
Boolean
Boolean.TRUE
42
int
42

Literal expressions can be evaluated immediately or deferred and can be either value or method expressions. When a literal expression is evaluated depends on where it is being used. If the tag attribute that uses the literal expression is defined as accepting a deferred, value expression then the literal expression references a value and is evaluated at the appropriate time, which is determined by where the expression is being used and what it is referring to.

In the case of a method expression, the method that is referenced is invoked and returns the specified String literal. The commandButton tag of the Guess Number application uses a literal method expression as a logical outcome to tell the JavaServer Faces navigation system which page to display next. See Navigation Model for more information on this example.

Resolving Expressions

The unified EL introduces a new, pluggable API for resolving expressions. The main pieces of this API are:

Most application developers will not need to use these classes directly unless they plan to write their own custom EL resolvers. Those writing JavaServer Faces custom components will definitely need to use ValueExpression and MethodExpression. This section details how expressions are resolved for the benefit of these developers. It does not explain how to create a custom resolver. For more information on creating custom resolvers, see the article, The Unified Expression Language.

Process of Expression Evaluation

When a value expression that is included in a page is parsed during an initial request for the page, a ValueExpression object is created to represent the expression. Then, the ValueExpression object's getValue method is invoked. This method will in turn invoke the getValue method of the appropriate resolver. A similar process occurs during a postback when setValue is called if the expression is an lvalue expression.

In the case of a method expression, a BeanELResolver is used to find the object that implements the method to be invoked or queried. Similarly to the process for evaluating value expressions, when a method expression is encountered, a MethodExpression object is created. Subsequently, either the invoke or getMethodInfo method of the MethodExpression object is called. This method in turn invokes the BeanELResolver object's getValue method. The getMethodInfo is mostly for use by tools.

After a resolver completes resolution of an expression, it sets the propertyResolved flag of the ELContext to true so that no more resolvers are consulted.

EL Resolvers

At the center of the EL machinery is the extensible ELResolver class. A class that implements ELResolver defines how to resolve expressions referring to a particular type of object or property. In terms of the following expression, a BeanELResolver instance is called the first time to find the base object, employee, which is a JavaBeans component. Once the resolver finds the object, it is called again to resolve the property, lName of the employee object.

${employee.lName} 

The unified EL includes a set of standard resolver implementations. Table 4-4 lists these standard resolvers and includes example expressions that they can resolve.

Table 4-4 Standard EL Resolvers
Resolver
Example Expression
Description
ArrayELResolver
${myArray[1]}
Returns the value at index 1 in the array called myArray
BeanELResolver
${employee.lName}
Returns the value of the lName property of the employee bean.
ListELResolver
${myList[5]}
Returns the value at index 5 of myList list
MapELResolver
${myMap.someKey}
Returns the value stored at the key, someKey, in the Map, myMap
ResourceBundleELResolver
${myRB.myKey}
Returns the message at myKey in the resource bundle called myRB

Depending on the technology using the unified EL, other resolvers might be available. In addition, application developers can add their own implementations of ELResolver to support resolution of expressions not already supported by the unified EL by registering them with an application.

All of the standard and custom resolvers available to a particular application are collected in a chain in a particular order. This chain of resolvers is represented by a CompositeELResolver instance. When an expression is encountered, the CompositeELResolver instance iterates over the list of resolvers and each resolver is consulted until a resolver that can handle the expression is found.

If an application is using JSP technology, the chain of resolvers includes the ImplicitObjectELResolver and the ScopedAttributeELResolver. These are described in the following section.

See section JSP2.9 of the JavaServer Pages 2.1 specification to find out the order in which resolvers are chained together in a CompositeELResolver instance.

Implicit Objects

The JSP expression language defines a set of implicit objects:

In addition, several implicit objects are available that allow easy access to the following objects:

Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.

JSP 2.1 provides two EL resolvers to handle expressions that reference these objects: ImplicitObjectELResolver and ScopedAttributeELResolver

A variable that matches one of the implicit objects is evaluated by ImplicitObjectResolver, which returns the implicit object. This resolver only handles expressions with a base of null. What this means for the following expression is that the ImplicitObjectResolver resolves the sessionScope implicit object only. Once the implicit object is found, the MapELResolver instance resolves the profile attribute because the profile object represents a map.

${sessionScope.profile} 

ScopedAttributeELResolver evaluates a single object that is stored in scope. Like ImplicitObjectELResolver, it also only evaluates expressions with a base of null. This resolver essentially looks for an object in all of the scopes until it finds it, according to the behavior of PageContext.findAttribute(String). For example, when evaluating the expression ${product}, the resolver will look for product in the page, request, session, and application scopes and will return its value. If product is not found, null is returned.

When an expression references one of the implicit objects by name, the appropriate object is returned instead of the corresponding attribute. For example, ${pageContext} returns the PageContext object, even if there is an existing pageContext attribute containing some other value.

Operators

In addition to the . and [] operators discussed in Value and Method Expressions, the JSP expression language provides the following operators, which can be used in rvalue expressions only:

The precedence of operators highest to lowest, left to right is as follows:

Reserved Words

The following words are reserved for the JSP expression language and should not be used as identifiers.

and   eq   gt   true   instanceof
or    ne   le   false  empty
not   lt   ge   null   div   mod 

Note that many of these words are not in the language now, but they may be in the future, so you should avoid using them.

Examples

Table 4-5 contains example EL expressions and the result of evaluating them.

Table 4-5 Example Expressions 
EL Expression
Result
${1 > (4/2)}
false
${4.0 >= 3}
true
${100.0 == 100}
true
${(10*10) ne 100}
false
${'a' < 'b'}
true
${'hip' gt 'hit'}
false
${4 > 3}
true
${1.2E4 + 1.4}
12001.4
${3 div 4}
0.75
${10 mod 4}
2
${!empty param.Add}
True if the request parameter named Add is null or an empty string
${pageContext.request.contextPath}
The context path
${sessionScope.cart.numberOfItems}
The value of the numberOfItems property of the session-scoped attribute named cart
${param['mycom.productId']}
The value of the request parameter named mycom.productId
${header["host"]}
The host
${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 and Chapter 7).

At first glance, functions seem similar to method expressions, but they are different in the following ways:

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"%>
...
    <c:when
      test="${f:equals(selectedLocaleString,
        localeString)}" > 

In this example, the expression referencing the function is using immediate evaluation syntax. A page author can also use deferred evaluation syntax to reference a function in an expression, assuming that the attribute that is referencing the function can accept deferred expressions.

If an attribute references a function with a deferred expression then the function is not invoked immediately; rather, it is invoked whenever the underlying technology using the function determines it should be invoked.

Defining Functions

To define a function you program it as a public static method in a public class. The mypkg.MyLocales class in the date example defines a function that tests the equality of two Strings as follows:

package mypkg;
public class MyLocales {

  ...
  public static boolean equals( String l1, String l2 ) {
    return l1.equals(l2);
  }
} 

Then you map the function name as used in the EL expression to the defining class and function signature in a TLD. The following functions.tld file in the date example maps the equals function to the class containing the implementation of the function equals and the signature of the function:

<function>
  <name>equals</name>
  <function-class>mypkg.MyLocales</function-class>
  <function-signature>boolean equals( java.lang.String,
    java.lang.String )</function-signature>
</function> 

No two functions within a tag library can have the same name.