JspExpression Language
The JSP expression language (EL) makes it very easy to access the data stored in JavaBean. JSP El can be used either to create arithmetic expressions or to create logical expressions. Integer numbers, floating-point numbers, strings, constants True, FALSE, and null can be used within a JSP El expression.
A simple syntax
Typically, when you need to specify a property value in a JSP tag, simply use the string:
<jsp:setpropertyName= "box"Property="Perimeter"value=" >
The JSP El allows you to specify an expression to represent a property value. A simple expression syntax is as follows:
${expr}
Where expr refers to an expression. The common operators in the JSP El are . and {}. These two operators allow you to access a wide variety of javabean properties through an inline JSP object.
For example, the above <jsp:setProperty> tags can be rewritten in the following form using an expression language:
<jsp:setpropertyName="box"Property="Perimeter"value="${2* Box.width+2*box.height} "/>
When the JSP compiler sees the "${}" format in the attribute, it generates code to evaluate the expression and produces a substitute for the value of the expression.
You can also use the expression language in the template text of a label. For example, the <jsp:text> tag simply inserts the text from its body into the JSP output:
<jsp:text>Hello jsp! </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>
You can use parentheses to organize sub-expressions in an El expression. For example, ${(1 + 2) * 3} equals 9, but ${1 + (2 * 3)} equals 7.
To deactivate an evaluation of an EL expression, you need to set the Iselignored property value to true using the page directive:
<%@="True|false" %>
This will cause the EL expression to be ignored. If set to False, the container evaluates the EL expression.
Base operator in El
The EL expression supports most of the arithmetic and logical operators provided by Java:
operator |
description |
. |
access a Bean property or a mapping entry |
[] |
Access An array or the elements of a linked list |
() |
organizes a sub-expression to change the precedence |
+ |
plus |
- |
minus or negative |
* |
multiply |
/or div |
except |
% or mod |
modulo |
= = or eq |
tests for equality |
! = or ne |
test for unequal |
< or LT |
test is less than |
> or GT |
test is greater than |
<= or le |
test is less than or equal to |
>= or GE |
test is greater than or equal to |
&& OR and |
test logic with |
| | or or |
test logic or |
! or not |
test negate |
empty |
test for null values |
Functions in the JSP el
The JSP El allows you to use functions in an expression. 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 (namespace), Func refers to the name of the function, param1 refers to the first argument, param2 refers to the second argument, and so on. For example, with a function fn:length, defined in the Jstl library, you can get the length of a string as follows:
${fn:length("Get my Length")}
To use any of the functions in the tag library, you need to install the libraries on the server, and then use the <taglib> tags to include them in the JSP file.
JSP El Hidden Objects
The JSP El supports the hidden objects listed in the following table:
implied Object |
Description |
Pagescope |
Page scope |
Requestscope |
Request Scope |
Sessionscope |
Session scope |
Applicationscope |
Application scope |
Param |
Parameters of the Request object, string |
Paramvalues |
Parameters of the Request object, a collection of strings |
Header |
HTTP message Header, string |
Headervalues |
HTTP information Header, string collection |
Initparam |
Context Initialization Parameters |
Cookies |
Cookie value |
PageContext |
PageContext of the current page |
You can use these objects in an expression just as you would with a variable. Here are a few examples to get a better understanding of the concept.
PageContext Object
The PageContext object is a reference to the PageContext object in the JSP. With the PageContext object, you can access the request object. For example, access the query string passed into the request object, like this:
${PageContext. Request. QueryString}
Scope Object
The Pagescope,requestscope,sessionscope,applicationscope variable is used to access variables stored at the various scope levels.
For example, if you need to explicitly access the box variable at the Applicationscope layer, you can do so by visiting: Applicationscope.box.
Param and Paramvalues objects
The Param and Paramvalues objects are used to access parameter values by using the Request.getparameter method and the Request.getparametervalues method.
For example, to access a parameter named order, you can use the expression: ${param.order}, or ${param["order"}.
Reprint: http://www.runoob.com/jsp/jsp-expression-language.html
El expression language