×
☰ See All Chapters

JSF Value Expression

Below is the syntax of JSF Value Expressions:

#{something}

#{something.something}

#{something ["something"] }

#{something ['something'] }

We can concatenate plain strings and value expressions by placing them next to each other. Below is an example for this.

<h:outputText value="#{messages.greeting}, #{user.name}!"/>

<h:outputText value="#{111},#{222}" />

You can use single quotes in value expressions if you delimit attributes with double quotes: value="#{user['password']}". Alternatively, you can switch single and double quotes: value='#{user["password"]}'.

Following things can be used inside the curly braces.

  • Literals.  

  • Operators.  

  • Variables.  

Literals

Following are the different literals which can be used inside the EL.

  • booleans :- ${true}  

  • integers :- ${1234}  

  • floating point numbers :- ${1.234}  

  • strings :- ${“helloworld”} 

Operators

Arithmetic Operators

Operation

Operator

Example

Addition

+

#{100+500}

Subtraction

-

#{800-200}

Multiplication

*

#{10*2}

Division

/

#{100/10}

Modulo

% or mod

#{100%10} or #{100 mod 10}

Relational Operators

Operation

Operator

Example

Greater than

> or  gt

#{700 > 500} or #{700 gt 500}

Lesser than

< or  lt

#{700 < 500} or #{700 lt 500}

Equal

== or  eq

#{500 == 500} or #{500 eq 500}

Greater than or equal

>= or  ge

#{500 >= 500} or #{500 ge 500}

Lesser than or equal

<= or  ge

#{500 <= 500} or #{500 le 500}

Not equals

!= or  ne

#{700 != 500} or #{700 ne 500}

Logical operators

Operation

Operator

Example

AND

&& or  and

#{var && var} or #{var and var}

OR

|| or  or

#{var || var} or #{var or var}

NOT

! or  not

#{!var} or #{not var}

JSF Value Expression Variables

The variables can be any of these two things.

  • predefined objects/ User defined objects (Initial term) 

  • Any attribute in the objects (Secondary term) 

jsf-value-expression-0
 

There are a number of predefined objects. Table below shows the complete list. For example, header['User-Agent'] is the value of the User-Agent parameter of the HTTP request that identifies the user’s browser.

If the initial term is not one of the predefined objects, the JSF implementation looks for it in the request, session, and application scopes, in that order. Those scopes are map objects that are managed by the servlet container. For example, when you define a managed bean, its name and value are added to the appropriate scope map. (In Background JSF implementation uses map objects to create scopes)

Variable Name

Description

header

Map of HTTP header parameters, containing only the first value for each name.

headerValues

Map of HTTP header parameters, yielding a String[] of all values of for given name.

param

Map of HTTP request parameters, containing only the first value for each name.

paramValues

Map of HTTP request parameters, yielding a String[] of all values of for given name.

cookie

Map of cookie name and values of current request

initParam

Map of initialization parameters.

requestScope

Map of all request scope attributes.

sessionScope

Map of all session scope attributes.

applicationScope

Map of all application scope attributes.

facesContext

FacesContext instance of the request.

view

UIViewRoor instance of the request.

 

Why would anyone write user["password"] when user.password is much easier to type? There are a number of reasons:

  • When you access an array or map, the [ ] notation is more intuitive. 

  • You can use the [ ] notation with strings that contain periods or dashes, for example, msgs["error.password"]. 

  • The [ ] notation allows you to dynamically compute a property: a[b.propname]. 

jsf-value-expression-1
 

All Chapters
Author