Value and Method Expressions
The EL defines two kinds of expressions: value expressions and method expressions. Value expressions can be evaluated to yield a value, and method expressions are used to reference a method.
The following topics are addressed here:
Value expressions can be further categorized into rvalue and lvalue expressions. An lvalue expression can specify a target, such as an object, a bean property, or elements of a collection, that can be assigned a value. An rvalue expression cannot specify such a target.
All expressions that are evaluated immediately use the
The former uses immediate evaluation syntax, whereas the latter uses
deferred evaluation syntax. The first expression accesses the
In the case of JavaServer Faces technology, the latter tag’s expression
is evaluated immediately during an initial request for the page. During
a postback request, this expression can be used to set the value of the
A top-level identifier (such as
To refer to these objects, you write an expression using a variable that
is the name of the object. The following expression references a managed
You can use a custom EL resolver to alter the way variables are
resolved. For instance, you can provide an EL resolver that intercepts
objects with the name
In the following expression, in which
Referencing Object Properties or Collection Elements
To refer to properties of a bean, static fields or methods of a class,
or items of a collection, you use the
To reference the
You can use double or single quotes for the
You can reference a static field or method using the syntax classname.field, as in the following example:
The classname is the name of the class without the package name. By
default, all the
If you are accessing an item in an array or list, you must use the
If you are accessing an item in a
The EL defines the following literals:
Here are some examples:
Parameterized Method Calls
The EL offers support for parameterized method calls.
In the first expression syntax, expr-a is evaluated to represent a bean object. The expression expr-b is evaluated and cast to a string that represents a method in the bean represented by expr-a. In the second expression syntax, expr-a is evaluated to represent a bean object, and identifier-b is a string that represents a method in the bean object. The parameters in parentheses are the arguments for the method invocation. Parameters can be zero or more values of expressions, separated by commas.
Parameters are supported for both value expressions and method
expressions. In the following example, which is a modified tag from the
The preceding example uses a value expression.
Consider the following example of a JavaServer Faces component tag that uses a method expression:
The EL expression
In the preceding example, you are passing the string
Where Value Expressions Can Be Used
Value expressions using the
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:
A tag attribute can be set in the following ways.
You can use the string concatenation operator += to create a single expression from what would otherwise be a composite expression. For example, you could change the composite expression
All 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
Another feature of the EL is its support of deferred method expressions. A method expression is used to refer to a public method of a bean and has the same syntax as an lvalue expression.
In JavaServer Faces technology, a component tag represents a component on a page. The component tag uses method expressions to specify methods that can be invoked to perform some processing for the component. These methods are necessary for handling events that the components generate and for validating component data, as shown in this example:
Because a method can be invoked during different phases of the lifecycle, method expressions must always use the deferred evaluation syntax.
Like lvalue expressions, method expressions can use the
Method expressions can be used only in tag attributes and only in the following ways:
A lambda expression is a value expression with parameters. The syntax is similar to that of the lambda expression in the Java programming language, except that in the EL, the body of the lambda expression is an EL expression.
For basic information on lambda expressions, see
A lambda expression uses the arrow token (
A lambda expression behaves like a function. It can be invoked immediately. For example, the following invocation evaluates to 7:
You can use a lambda expression in conjunction with the assignment and semicolon operators. For example, the following code assigns the previous lambda expression to a variable and then invokes it. The result is again 7:
A lambda expression can also be passed as an argument to a method and be invoked in the method. It can also be nested in another lambda expression.