1. Introduction

Expression Language (EL), is a scripting language that’s seen adoption within many Java frameworks, such as Spring with SpEL and JBoss with JBoss EL.

In this article, we'll focus at the JSF’s implementation of this scripting language – Unified EL.

EL is currently in version 3.0, a major upgrade that allows the processing engine to be used in standalone mode – for example, on the Java SE platform. Prior versions were dependent on a Jakarta EE-compliant application server or web container. This article discusses EL version 2.2.

2. Immediate and Deferred Evaluation

The primary function of EL in JSF is to connect the JSF view (usually XHTML markup) and the java-based back-end. The back-end can be user-created managed beans, or container-managed objects like the HTTP session.

We will be looking at EL 2.2. EL in JSF comes in two general forms, immediate syntax EL and deferred syntax EL.

2.1. Immediate Syntax EL

Otherwise known as JSP EL, this is a scripting format that’s a holdover from the JSP days of java web application development.

The JSP EL expressions start with the dollar sign ($), then followed by the left curly bracket ({), then followed by the actual expression, and finally closed with the right curly bracket (}):

${ELBean.value > 0}

This syntax:

  1. Is evaluated only once (at the beginning) in the lifecycle of a page. What this means is that the value that is. Being read by the expression in the example above must be set before the page is loaded.
  2. Provides read-only access to bean values.
  3. And as a result, requires adherence to the JavaBean naming convention.

For most uses, this form of EL is not very versatile.

2.2. Deferred Execution EL

Deferred Execution EL is the EL designed for JSF proper. It’s major syntactical difference with JSP EL is that it’s marked with a “*#”* instead of a “*$*“.

#{ELBean.value > 0}

Deferred EL:

  1. Is in sync with the JSF lifecycle. This means that an EL expression in deferred EL is evaluated at different points in the rendering of a JSF page (at the beginning and the end).
  2. Provides read and write access to bean values. This allows one to set a value in a JSF backing-bean (or anywhere else) using EL.
  3. Allows a programmer to invoke arbitrary methods on an object and depending on the version of EL, pass arguments to such methods.

Unified EL is the specification that unifies both deferred EL and JSP EL, allowing both syntax in the same page.

3. Unified EL

Unified EL allows two general flavors of expressions, value expressions and method expressions.

And a quick note – the following sections will show some examples, which are all available in the app (see the Github link at the end) by navigating to:

http://localhost:8080/jsf/el_intro.jsf

3.1. Value Expressions

A value expression allows us to either read or set a managed bean property, depending on where it’s placed.

The following expression reads a managed bean property onto the page:

Hello, #{ELBean.firstName}

The following expression however, allows us to set a value on the user object:

<h:inputText id="firstName" value="#{ELBean.firstName}" required="true"/>

The variable must follow JavaBean naming convention to be eligible for this kind of treatment. For the value of the bean to be committed, the enclosing form just needs to be saved.

3.2. Method Expressions

Unified EL provides method expressions to execute public, non-static methods from within a JSF page. The methods may or may not have return values.

Here's a quick example:

<h:commandButton value="Save" action="#{ELBean.save}"/>

The save() method being referred to is defined on a backing bean named ELBean.

Starting from EL 2.2, you can also pass arguments to the method that’s accessed using EL. This can allow us to rewrite our example thus:

<h:inputText id="firstName" binding="#{firstName}" required="true"/>
<h:commandButton value="Save"
  action="#{ELBean.saveFirstName(firstName.value.toString().concat('(passed)'))}"/>

What we’ve done here, is to create a page-scoped binding expression for the inputText component and directly pass the value attribute to the method expression.

Note that the variable is passed to the method without any special notation, curly braces or escape characters.

3.3. Implicit EL Objects

The JSF EL engine provides access to several container-managed objects. Some of them are:

  • #{Application}: Also available as the #{servletContext}, this is the object representing the web application instance
  • #{applicationScope}: a map of variables accessible web application-wide
  • #{Cookie}: a map of the HTTP Cookie variables
  • #{facesContext}: the current instance of FacesContext
  • #{flash}: the JSF Flash scoped-object
  • #{header}: a map of the HTTP headers in the current request
  • #{initParam}: a map of the context initialization variables of the web application
  • #{param}: a map of the HTTP request query parameters
  • #{request}: the HTTPServletRequest object
  • #{requestScope}: a request-scoped map of variables
  • #{sessionScope}: a session-scoped map of variables
  • #{session}: the HTTPSession object
  • #{viewScope}: a view (page-) scoped map of variables

The following simple example lists all the request headers and values by accessing the headers implicit object:

<c:forEach items="#{header}" var="header">
   <tr>
       <td>#{header.key}</td>
       <td>#{header.value}</td>
   </tr>
</c:forEach>

4. What You Can Do in EL

In its versatility, EL can be featured in Java code, XHTML markup, Javascript and even in JSF configuration files like the faces-config.xml file. Let's examine some concrete use-cases.

4.1. Use EL in Page Markup

EL can be featured in standard HTML tags:

<meta name="description" content="#{ELBean.pageDescription}"/>

4.2. Use EL in JavaScript

EL will be interpreted when encountered in Javascript or