Contents | Prev | Next


EL Expression

Contains an expression in the JSP Expression Language (EL). It allows easy access to application data stored in JavaBeans components.

Syntax (JSP and XML)

${Expression}
Expression = {(ChoiceExpression | (Expression BinaryOp Expression)	
            (UnaryOp Expression) | Value }
ChoiceExpression = Expression ? Expression : Expression
BinaryOp (JSP syntax) = and | && | or | || | '+' | '-' | * | / | did       	
                        | % | mod | > | gt | < | lt | >= | ge | <= |  	
                        le | == | eq | != | ne
BinaryOp (XML syntax) = and | && | or | || | '+' | '-' | * | / | div 	
                        | % | mod | gt |  lt | ge |  le | == | eq | ne
UnaryOp = {'-' | ! | not | empty }
Value = {ValuePrefix | (Value ValueSuffix) }
ValuePrefix = {Literal | '('Expression')' | ImplicitObject | 	
            Java language identifier | FuncInvocation }
ValueSuffix = { . Identifier | '['Expression']'}
FuncInvocation = [Identifier :]Identifier '('[Expression [',' 	
                  Expression]+ ]')'
Literal = {true|false} | (0-9)+ | FloatingPtLiteral | StringLiteral 	
            | null
FloatingPtLiteral = [0-9]+ . (0-9)+ [Exponent] | (0-9)+ [Exponent]
Exponent = {e | E} ['+' | -] (0-9)+
StringLiteral = {'[^(' | \) | \' | \\]+' | "[^(" | \) | \" | \\]+"}

Examples

The following three examples show tag attributes using EL expressions to access values stored in JavaBeans components. TABLE 1 shows how some example EL expressions evaluate.

<a:tag value="${x+y}" />	
<a:tag value="${first} ${last}" />	
<a:tag>${x+y}</a:tag>
TABLE 1       Expression language examples

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.forwardservlet_path

Description

A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property.

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> 

The JSP expression evaluator is responsible for handling EL expressions, which are enclosed by the ${ } characters and can include literals. Here's an example:

<c:if test="${bean1.a < 3}" >
  ...
</c:if> 

Any value that does not begin with ${ is treated as a literal and is parsed to the expected type using the PropertyEditor for the type:

<c:if test="true" >
...
</c:if> 

Literal values that contain the ${ characters must be escaped as follows:

<mytags:example attr1="an expression is ${'${'}true}" /> 

EL expressions can be used:

The value of an expression in static text is computed and inserted into the current output. 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.

There are three ways to set a tag attribute value:

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 JSP2.8 of the JSP 2.0 specification for the complete type conversion rules.

Variables

The Web container evaluates a variable that appears in an expression by looking up its value according to the behavior of PageContext.findAttribute(String). For example, when evaluating the expression ${product}, the container 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. A variable that matches one of the implicit objects described in Implicit Objects will return that implicit object instead of the variable's value.

Properties of variables are accessed using the . operator and can be nested arbitrarily.

The JSP expression language unifies the treatment of the . and [] operators. expr-a.identifier-b is equivalent to expr-a["identifier-b"]; that is, the identifier identifier-b is used to construct a literal whose value is the identifier, and then the []operator is used with that value.

To evaluate expr-a[expr-b], evaluate expr-a into value-a and evaluate expr-b into value-b. If either value-a or value-b is null, return null.

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.

When an expression references one of these 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.

Literals

The JSP expression language defines the following literals:

Operators

In addition to the . and [] operators discussed in Variables, the JSP 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 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.

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.

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 following example page imports the /functions library and invokes the function equals in an expression:

<%@ taglib prefix="f" uri="/functions"%>
...
    <c:when
      test="${f:equals(selectedLocaleString,
        localeString)}" > 

To define a function you program it as a public static method in a public class. The mypkg.MyLocales class example below 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 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> 

A tag library can have only one function element that has any given name element.

See Also



Contents | Prev | Next

Copyright © 2004, Sun Microsystems, Inc. All rights reserved.