Unified EL (Unified Expression Language)

Unified EL (Unified Expression Language)


Posted in : Java Posted on : May 2, 2012 at 6:29 PM Comments : [ 0 ]

In this tutorial you will learn about the Unified Expression Language in JSP.

Unified EL (Unified Expression Language)

In this tutorial you will learn about the Unified Expression Language in JSP.

Unified Expression Language is an Union of the expression language. Expression Language provides the simple expressions that helps the developer to read data dynamically from the components of JavaBeans. This feature were offered in the JSP 2.0 and is made a primary new feature of JSP 2.1. Unified EL permits the developer to use simple expression to use the following task :

  • To read the application data from JavaBeans components dynamically, various data structures and implicit objects.
  • To write data dynamically to JavaBeans components.
  • invoke arbitrary static and public methods.
  • To perform an arithmetic operation dynamically.

For the custom tag developers the Unified EL permits to define which of following kinds of expressions will accepted by the attributes of custom tag :

  • Immediate evaluate expressions or deferred evaluation expressions. JSP engine evaluates the Immediate evaluate expressions without making delay. Whereas, using the expression language an inherent technology evaluates the deferred evaluation expressions subsequent to a reference time (later).
  • Value expression or method expression. Value expression addresses the data and method expression evokes a method. Immediate evaluate expressions uses the ${ } delimiters and are always Rvalue expressions and deferred evaluate expressions uses the #{ } delimiters and can act as both Rvalue & Lvalue expressions.
  • Rvalue expression or Lvalue expression. Rvalue expression can only reads the value whereas, Lvalue expression can both read and write that value to an external object.

Features of the Unified EL :

  • Unified EL is a pluggable API for resolving expressions.
  • It provides the facility for expressions to set the values and to evoke methods.
  • It provides the facility to use the deferred expressions with iteration tags of JSTL.
  • Deferred (later) evaluation of expression.

Unified EL objects reference using Value expressions.

Following objects and their properties or attributes can be addressed by the both Rvalue and Lvalue expressions:

JavaBeans component : If a JavaBeans component called "dev" can be referred by writing an expression as : ${dev}.

How to refer to object properties using Value expressions ?

A (.) dot operator or [] is used to refer to properties of a bean or a collection, or an attributes of an implicit object for example if you want to refer the property "name" of dev bean you can write an expression as : ${dev.name} or the expression ${dev ["name"]}.

Accessing of list or array items as :

${dev.result[1]} or ${dev.result.marks} here, result is a property and marks is a coerced int.

However, in Map item can be accessed by using String literal key coercion is not needed. e.g

${dev.result["marks"]}

Following literals are defined by the Unified EL :

  • Boolean : true and false.
  • Integer : as in java.
  • Floating point : as in java.
  • String : with single quotes and double quotes.
  • Null : null

Deactivating Expression Evaluation

An expression evaluation can be escaped using the " \ " character. Using this escaping character you can escape the evaluation of characters ${ or #{ in the JSP page. for example <c:out value=" \ ${str " />. Another way of deactivating the EL expression is to define a page directive <%@ page isELIgnored="true" on the beginning of the JSP page, and the false value specified that the EL expressions will be evaluated if the attribute rtexprvalue is set to true.

Operators in JSP Expression Language

In JSP EL allows for the following operators that can be used in only Rvalue expressions :

  • Arithmetic : +, - (binary & unary), *, / and div, % and mod.
  • Logical : and, &&, or, ||, not, !.
  • Relational : = =, eq, !=, ne, <, lt, >, gt, <=, ge, >=, le.
  • Empty : This operator is a prefix operation which specifies whether a value is null or empty.
  • Conditional : A ? B : C, a ternary operation whether B or C will be evaluated depends on the result of the evaluation of A.

Following is the operators precedence from highest to lowest, left to right :

  • [] .
  • () (used to change the precedence of operators)
  • - (unary) not ! empty
  • * / div % mod
  • + - (binary)
  • < > <=  >=  lt gt le ge
  • = =  !=  eq  ne
  • &&  and
  • || or
  • ? :

Reserved Words in JSP

Following table specifies the reserved word for JSP expression language :

and or not eq
ne lt gt le
ge true false null
instanceof empty div mod

How to define a function

Functions are the static methods and to define functions it should be in the public class and map the function name and signature into the TLD file using which they will be used in the EL expression :

for Example : To define a function like equal() should first define in the public class as :

package devmanuals;
public class StringMatch
{
// some more statements
public static boolean stringMatch(String str1, String str2)
{
return str1.equals(str2);
}
}

And mapping of the function name and function signature in the TLD file should be as follows :

<function>
<name>stringMatch</name>
<function-class>devmanuals.StringMatch</function-class>
<function-signature>boolean stringMatch( java.lang.String,
java.lang.String )</function-signature>
</function>

Functions are different from the method expressions in the following ways :

Functions Method Expressions
These are static methods which returns some value. These are non-static, arbitrary public methods on objects.
These can be identified statically at translation time Methods can be identified dynamically at runtime.
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics