JSF Expression Language – JSF EL

Filed Under: JSF

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


<h:commandButton id="sub" value="Add Details" action="#{car.add}"> </h:commandButtton>

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.


<h:inputText id="speed" value="#{car.calculatespeed(‘70.2’)}"></h:inputText>

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.


<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
	<title>Facelet Title</title>
</h:head>
<h:body>
        Car Name:#{car.cname}
        <br>
	<br>
      
        Color:#{car.color}
        <br>
	<br>
        
        Model Number:#{car.modelno}
        <br>
	<br>
        
        Speed:#{car.calculatespeed(45.20)}
       
    </h:body>
</html>

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


package com.journaldev.jsf.beans;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class Car {

	private String cname = "Polo";
	private String color = "White";
	private String modelno = "P245";

	public Car() {
	}

	public Car(String cname, String color, String modelno) {
		this.cname = cname;
		this.color = color;
		this.modelno = modelno;
	}

	public String getCname() {
		return cname;
	}

	public void setCname(String cname) {
		this.cname = cname;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getModelno() {
		return modelno;
	}

	public void setModelno(String modelno) {
		this.modelno = modelno;
	}

	public float calculatespeed(float speed) {

		float totalspeed = speed + 10;
		return totalspeed;

	}

}

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

JSF Expression Language

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

Reference: Oracle Documentation

Comments

  1. Demba KANDE says:

    Bonjour, je suis Demba KANDE étudiant en Master II à l’université Alioune DIOP de Bambey (Sénégal).
    Je suis vos tutoriels et j’ai même débuté une application de gestion des commandes et livraisons des équipements de (UADB), pour la validation du module JEE. Mais pour le moment suis bloqué sur la récupération des clés étrangers à partir de la base de données, de les convertir et les affichés au coté vue sous forme de seletmanyone.
    Bien Cordialement,

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages