Java Reference
In-Depth Information
Chapter 3
Expression Language (EL)
The Expression Language (EL) 3.0 release is full of features that help enrich the language by making the language
more powerful and by increasing productivity for developers. EL has always been a sweet spot for JSP and JSF
developers, allowing the use of a very productive scripting language that provides the ability to interact directly with
server-side Java within web views. This release enhances the language by adding features such as collection support
and lambda expressions, which helps bring EL in line with more modern scripting languages. The release also adds
support for working within a stand-alone environment. It is now possible to evaluate expressions, set variables, work
with static fields and methods, and define functions.
Lambda Expressions
The EL 3.0 release brings support for lambda expressions. Simply put, a lambda expression behaves much like a
function, because it accepts parameters and returns a value. Lambdas provide a way to encapsulate a function by
assigning it to an identifier. That said, using lambdas, functions can be passed around as objects and even used as
parameters to other functions. A lambda expression accepts parameters that go on the left side of the -> operator, and
the function or body of the expression is placed on the right side of the -> operator. Lambda expressions in EL look
much like those that are part of JDK 8, but the difference is that the body portion of an EL lambda is an EL expression
that can utilize all the features provided by EL 3.0. For instance, all the reserved words that are available to EL, such as
gt , lt , and eq , are available for use within a lambda expression. That said, let's take a look at some examples!
Lambda Concept and Examples
The following lambda expression accepts two parameters and returns the product:
((a,b) -> a * b (3,4)) -- Returns 12
Breaking down the example, the (a,b) part of the expression consists of the parameters that are accepted by
the lambda. The -> operator separates the parameters from the body of the lambda, which is a * b . Lastly, (3,4)
are the values that are passed to the expression, and then the entire expression is surrounded by parentheses. The
values being passed to the expression are optional, because the lambda could be assigned to an identifier instead. If
a lambda is assigned to an identifier, that identifier can later be treated as a function, passing the values you want to
utilize within the lambda as parameters. You'll see an example of this in the following section.
As mentioned previously, EL can be used within lambda expressions. Therefore, any of the EL operators are valid
for use, so something like the following is possible:
a -> a == 0 ? 1 : a * a
 
Search WWH ::




Custom Search