JSF Expression Language

Jsf is provided with rich expression language, which helps to directly map the UI component value to Bean properties.


<h:outputText value="#{userBean.firstName}"/>

Here outputText is directly mapped with firstName property value of userBean.

How EL Expression Is Evaluated:

EL is evaluated at runtime. First part-userBean is considered as base and firstName is property of that base.

Now suppose userBean is defined in sessionScope.

When expression is evaluted then getAttribute("userBean") will get executed on the ServletRequest. Since the bean is managed in sessionScope, so it is not found in Servlet Request. Next EL obtains the Map from the getViewMap( ) method of the UIViewRoot, but again bean not found as it is in sessionScope. Next getAttribute("userBean") on the HttpSession will get executed and now the bean found and base is obtained, it then proceeds to resolve properties against the base. With firstName, the getFirstName( ) method is called on the UserBean instance.

Smilar way setter method will get executed for below UI Component:

<h:inputText value="#{userBean.firstName}"/>

We can perform Arithmetic, logical, relational operations with EL.

Arithmetic +, -, *, / (or div), % (or mod)
Relational == (or eq ), != (or ne), < (or lt), > (or gt), <= (or le), >= (or ge)
Logical && (or and), || (or or), ! (or not)
Conditional A ? B : C (Evaluate A to Boolean. If true, evaluates and returns B. Otherwise, evaluates and returns C.)
Empty = empty A (If A is null or is an empty string, array, Collection, returns true. Otherwise returns false.)

Comparison with Jsp Expression language

In comparison to jsp expression language jsf evaluates the expression for a variable when it is going to be used on the page, whereas in jsp expression will get evaluated as soon as it appear in the page while rendering.