JSF Expression Language enables users to access the data dynamically from the JavaBeans components using various expressions.

JSF Expression Language

JSF Expression Language or JSF EL supports the following kinds of expressions.

  1. Immediate value expressions or Deferred value expressions
  2. Value expression or method expression
  3. rvalue or lvalue expressions

JSF EL – Immediate value expressions

Immediate expressions are evaluated and results are rendered as soon as the page is displayed initially. The syntax for immediate evaluation is

${}

Immediate evaluation expressions can be used within a template text or as tag attribute value that accepts runtime expressions. These type of expressions are read-only.

JSF EL – Deferred value expressions

Deferred expressions are evaluated during the lifecycle phase whenever it is needed. The syntax for deferred evaluation is

#{expression}

Deferred value expressions can be of two types

  1. Value expressions
  2. Method expressions

JSF Expression Language – Value Expressions

Value expressions usually fetch a value or set a value. These expressions can be further categorized into rvalue and lvalue expressions.

lvalue expressions can both read and write data whereas rvalue expressions can only read data.

For example, if we want to fetch the name of the car from the managed bean

${car.cname}: for immediate evaluation

#{car.cname}: for deferred evaluation

The immediate evaluation ${car.cname} acts as an rvalue expression whereas in some cases during a request this expression can be used to set the value to the cname field and hence acts as an lvalue expression.

JSF EL – Referencing objects with value expressions

The value expressions can be referred to managed bean components, collections, enumeration types and implicit objects types.

For example : ${car}

This expression refers to the managed bean component named “car”. While evaluating this expression the container searches for car in the page, request or session and returns the value. If the car is not found then the value returned is null.

Assuming there is list named carnames containing the following values,

public List carnames={"Santro","Zen","Polo","Innova"}

Suppose if we want to access the car named Polo then we use the expression

${carnames [2]}

where [2] refers to the third element in the list with value “Polo”

JSF EL – Referring object properties using value expressions

To access the managed bean properties, elements in a collection or implicit objects we use . or [] notation.

If we consider an example where we want to access the cname of the car managed bean then we use the expression

${car.cname} or ${car[“cname”]}

If we want to access the elements in an array or a collection then we use

${car.carnames[3]}

where car is the managed bean and carnames is the list from which we are accessing the third element. A literal value can be used that can be converted to int without quotes as ${car.carnames.nval} assuming that nval can be converted to int data type.

In order to access element from a Map, then the quotes should be specified as

${car.cnames[“nval”]} where car is the bean, cnames is a map.

The EL supports the Boolean, Integer, Floating, null and String data types.

JSF Expressions Language – Method Expressions

JSF EL method expression allows user to invoke a public method of the bean that returns the result. Method expressions are necessary for validating the data component and handling events.

Consider an example of invoking a method in a bean

Here on click of “Add Details” button we invoke the add method of the car bean.

Method expressions can use . or [] operator as #{car.add} or #{car[“add”]}.

JSF EL Parameterized methods

The Expression Language supports the methods with parameters. Parameters are supported for both value and method expressions.
Consider an example of how to invoke a method with parameters.

Here we are calling the calculatespeed method of bean car by passing parameter speed having the value 70.2.

Now let’s see a simple example where we will see the use of JSF Expression Language.

Create a JSF page car.xhtml as shown below.

Here we are invoking the calculate speed method by passing speed value 45.20 and fetching the values for cname, color and modelno attributes.

Create managed bean Car.java as

Now run the application to see the following output in the browser.

JSF-Expression-Language-Example

Below image shows the final project structure in Eclipse.
JSF-Expression-Language-Project
Finally, you can download the JSF EL example project from below link and play around with it to learn more.

Reference: Oracle Documentation

By admin

Leave a Reply

%d bloggers like this: