JSP系列:JSP进阶-EL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP系列:JSP进阶-EL相关的知识,希望对你有一定的参考价值。

1、简单语法

Expression Language(EL) was added to JSP 2.0 specification. The purpose of EL is to produce scriptless JSP pages. The syntax of EL in a JSP is as follows:

    ${expr}

Here expr specifies the expression itself.

(1)JSP EL中常用的两个操作符就是:“.”和“[]”。

The most common operators in JSP EL are . and []. These two operators allow you to access various attributes of Java Beans and built-in JSP objects.

(2)如果JSP compiler看到${},就会计算其中表达式的值并进行输出。

When the JSP compiler sees the ${} form in an attribute, it generates code to evaluate the expression and substitues the value of expresson.

(3)如果想控制JSP EL表达式是否有效,需要在page指令中设置。

To deactivate the evaluation of EL expressions, we specify the isELIgnored attribute of the page directive as below: 

<%@ page isELIgnored ="true|false" %>

The valid values of this attribute are true and false. If it is true, EL expressions are ignored when they appear in static text or tag attributes. If it is false, EL expressions are evaluated by the container.

 

2、Basic Operators in EL

JSP Expression Language (EL) supports most of the arithmetic and logical operators supported by Java.Below is the list of most frequently used operators:


Basic Operators in EL
序号OperatorDescription
01.Access a bean property or Map entry
02[ ]Access an array or List element
03( )Group a subexpression to change the evaluation order
04(算术)+Addition
05(算术)-Subtraction or negation of a value
06(算术)*Multiplication
07(算术)/ or divDivision
08(算术)% or modModulo (remainder)
09(关系)== or eqTest for equality
10(关系)!= or neTest for inequality
11(关系)< or ltTest for less than
12(关系)> or gtTest for greater than
13(关系)<= or leTest for less than or equal
14(关系)>= or geTest for greater than or equal
15(逻辑)&& or andTest for logical AND
16(逻辑)|| or orTest for logical OR
17(逻辑)! or notUnary Boolean complement
18emptyTest for empty variable values


3、JSP EL Implicit Objects

The JSP expression language supports the following implicit objects:

技术分享


JSP EL Implicit Objects
序号Implicit objectDescription
01pageScopeScoped variables from page scope
02requestScopeScoped variables from request scope
03sessionScopeScoped variables from session scope
04applicationScopeScoped variables from application scope
05paramRequest parameters as strings
06paramValuesRequest parameters as collections of strings
07headerHTTP request headers as strings
08headerValuesHTTP request headers as collections of strings
09initParamContext-initialization parameters
10cookieCookie values
11pageContextThe JSP PageContext object for the current page

You can use these objects in an expression as if they were variables. Here are few examples which would clear the concept:


3.1、The pageContext Object

此处的pageContext是EL表达式的implicit Object,它正好对应JSP的九个内置对象中的pageContext。

The pageContext object gives you access to the pageContext JSP object.

Through the pageContext object, you can access the request object. For example, to access the incoming query string for a request, you can use the expression:

    ${pageContext.request.queryString}


3.2、The Scope Objects

The pageScope, requestScope, sessionScope, and applicationScope variables    provide access to variables stored at each scope level.

For example, If you need to explicitly access the box variable in the application scope, you can access it through the applicationScope variable as applicationScope.box.


3.3、The param and paramValues Objects

The param and paramValues objects give you access to the parameter values normally available through the request.getParameter and request.getParameterValues methods.

For example, to access a parameter named "order", use the expression ${param.order} or ${param["order"]}.


3.4、header and headerValues Objects

The header and headerValues objects give you access to the header values normally available through the request.getHeader and request.getHeaders methods.

For example, to access a header named "user-agent", use the expression ${header.user-agent} or ${header["user-agent"]}.


4、JSP EL Important Points

(1)EL expressions are always within curly braces prefixed with $ sign, for example ${expr}

(2)We can disable EL expression in JSP by setting JSP page directive isELIgnored attribute value to TRUE.

(3)JSP EL can be used to get attributes, header, cookies, init params etc, but we can‘t set the values.

(4)JSP EL implicit objects are different from JSP implicit objects except pageContext, don‘t get confused.

(5)JSP EL pageContext implicit object is provided to get additional properties from request, response etc, for example getting HTTP request method.

(6)JSP EL is NULL friendly, if given attribute is not found or expression returns null, it doesn‘t throw any exception. For arithmetic operations, EL treats null as 0 and for logical operations, EL treats null as false.

(7)The [] operator is more powerful than dot operator because we can access list and array data too, it can be nested and argument to [] is evaluated when it’s not string literal.

(8)If you are using Tomcat, the EL expressions are evaluated using

org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate()

method.

(9)We can use EL functions to call method from a java class.

文章参考:

http://beginnersbook.com/2013/11/jsp-expression-language-el/

http://www.journaldev.com/2064/jsp-expression-language-el-example-tutorial




以上是关于JSP系列:JSP进阶-EL的主要内容,如果未能解决你的问题,请参考以下文章

JSP系列:JSP进阶-EL表达式和JSP标签

JSP 中EL表达式用法

EL表达式jsp标签JSTL标签

JSP-EL表达式

JSP 中EL表达式用法详解

JSP 中EL表达式用法详解