JSP Expression Language
JSP Expression Language (EL) makes accessing data stored in JavaBeans very simple. JSP EL can be used to create both arithmetic and logical expressions. Integers, floating point numbers, strings, constants true, false, and null can be used in JSP EL expressions.
A simple syntax
Typically, when you need to specify an attribute value in a JSP tag, you simply use a string:
<jsp:setProperty name="box" property="perimeter" value="100"/>
JSP EL allows you to specify an expression formula to represent attribute values. A simple expression syntax is as follows:
${expr}
where expr refers to the expression. Common operators in JSP EL are "." and "[]". These two operators allow you to access a variety of JavaBean properties through embedded JSP objects.
For example, the above <jsp:setProperty> tag can be rewritten into the following form using expression language:
<jsp:setProperty name="box" property="perimeter" value="${2*box.width+2*box.height}"/>
When the JSP compiler sees "${}" in the property After formatting, it generates code to evaluate the expression and produce a substitute for the expression's value.
You can also use expression languages in the template text of labels. For example, the <jsp:text> tag simply inserts the text in its body into the JSP output:
<jsp:text> <h1>Hello JSP!</h1> </jsp:text>
Now, use an expression in the body of the <jsp:text> tag, like this:
<jsp:text> Box Perimeter is: ${2*box.width + 2*box.height} </jsp:text>
Parents can be used in EL expressions to organize subexpressions. For example, ${(1 + 2) * 3} is equal to 9, but ${1 + (2 * 3)} is equal to 7.
If you want to disable the evaluation of EL expressions, you need to use the page directive to set the isELIgnored attribute value to true:
<%@ page isELIgnored ="true|false" %>
In this way, the EL expression will be ignored. If set to false, the container will evaluate the EL expression.
Basic operators in EL
EL expressions support most of the arithmetic and logical operators provided by Java:
Operator | Description |
---|---|
. | Access a Bean property or a mapping entry |
[] | Access elements of an array or linked list |
( ) | Structure a subexpression to change precedence |
+ | add |
- | Subtract or negative |
* | take |
/ or div | remove |
% or mod | Modulo |
== or eq | Test for equality |
!= or ne | Test whether it is not equal |
< or lt | Test whether it is less than |
> or gt | Test whether it is greater than |
<= or le | Test whether it is less than or equal to |
>= or ge | Test whether it is greater than or equal to |
&& or and | Test logic and |
|| or or | Test logic or |
! or not | Test negation |
empty | Test whether the value is empty |
Functions in JSP EL
JSP EL allows you to use functions in expressions. These functions must be defined in the custom tag library. The syntax for using the function is as follows:
${ns:func(param1, param2, ...)}
ns refers to the namespace, func refers to the name of the function, param1 refers to the first parameter, param2 refers to the second parameter, and And so on. For example, there is the function fn:length, defined in the JSTL library, which can be used to obtain the length of a string as follows:
${fn:length("Get my length")}
To use the functions in any tag library, you need to install these libraries on the server , and then include these libraries in the JSP file using the <taglib> tag.
JSP EL implicit objects
JSP EL supports the implicit objects listed in the following table:
Implicit object | Description |
---|---|
page scope | |
request scope | |
session scope | |
application scope | |
Parameters of the Request object, string | |
Parameters of Request object, string collection | |
HTTP header, string | |
HTTP header, string set | |
Context initialization parameters | |
Cookie value | |
pageContext |