Introduction to EL Expressions

1. Definition of JSP EL language


EL (Expression Language) Purpose: To make JSP easier to write.

The expression language, inspired by the ECMAScript and XPath expression languages, provides a way to simplify expressions in JSP. It is a simple language based on available namespaces (PageContext properties), nested properties and accessors to collections, operators (arithmetic, relational and logical), mappings to static methods in Java classes Extension functions and a set of implicit objects.

EL provides the ability to use runtime expressions outside the scope of JSP scripting elements. A scripting element is an element in a page that can be used to embed Java code in a JSP file. They are typically used for object manipulation and to perform computations that affect the generated content. JSP 2.0 adds EL expressions as a scripting element.

 

2. Introduction to JSP EL

1. Syntax structure
${expression}
2. [ ] and . Operator
EL provides two operators "." and "[ ]" to access data.
When the property name to be accessed contains some special characters, such as . or ? and other symbols that are not letters or numbers, you must use "[ ]". For example:
${user.My-Name} should be changed to ${user["My-Name"] }
If you want to dynamically retrieve the value, you can use "[ ]" to do it, but "." cannot be dynamically retrieved value. For example:
data in ${sessionScope.user[data]} is a variable
3. The method for variable
EL to access variable data is very simple, for example: ${username}. It means to take out a variable named username in a scope.
Because we did not specify which scope of username, it will be searched from the scope of Page, Request, Session, and Application in order.
If the username is found on the way, it will be returned directly, and it will not continue to search, but if all the ranges are not found, it will return null.
Property scope name in EL
Page PageScope
Request RequestScope
Session SessionScope
Application ApplicationScope

2. Valid expressions in JSP EL

Valid expressions can contain literals, operators, variables (object references), and function calls. We'll look at each of these valid expressions separately:

1. Text

The JSP Expression Language defines the following literals that can be used in expressions:

 

literal literal value

Boolean

true and false

Integer

与 Java 类似。可以包含任何正数或负数,例如 24、-45、567

Floating Point

与 Java 类似。可以包含任何正的或负的浮点数,例如 -1.8E-45、4.567

String

任何由单引号或双引号限定的字符串。对于单引号、双引号和反斜杠,使用反斜杠字符作为转义序列。必须注意,如果在字符串两端使用双引号,则单引号不需要转义。

Null null

 

2、操作符

JSP 表达式语言提供以下操作符,其中大部分是 Java 中常用的操作符:

 

术语 定义

算术型

+、-(二元)、*、/、div、%、mod、-(一元)

逻辑型

and、&&、or、||、!、not

关系型

==、eq、!=、ne、、gt、<=、le、>=、ge。可以与其他值进行比较,或与布尔型、字符串型、整型或浮点型文字进行比较。

空操作符是前缀操作,可用于确定值是否为空。

条件型 A ?B :C。根据 A 赋值的结果来赋值 B 或 C。

 

3、隐式对象

JSP 表达式语言定义了一组隐式对象,其中许多对象在 JSP scriplet 和表达式中可用:

 

 

 

 

pageContext

JSP 页的上下文。它可以用于访问 JSP 隐式对象,如请求、响应、会话、输出、servletContext 等。例如,${pageContext.response} 为页面的响应对象赋值。

 

此外,还提供几个隐式对象,允许对以下对象进行简易访问:

 

术语 定义

param

将请求参数名称映射到单个字符串参数值(通过调用 ServletRequest.getParameter (String name) 获得)。getParameter (String) 方法返回带有特定名称的参数。表达式 $(param.name) 相当于 request.getParameter (name)。

paramValues

将请求参数名称映射到一个数值数组(通过调用 ServletRequest.getParameter (String name) 获得)。它与 param 隐式对象非常类似,但它检索一个字符串数组而不是单个值。表达式 ${paramvalues.name) 相当于 request.getParamterValues(name)。

header

将请求头名称映射到单个字符串头值(通过调用 ServletRequest.getHeader(String name) 获得)。表达式 ${header.name} 相当于 request.getHeader(name)。

headerValues

将请求头名称映射到一个数值数组(通过调用 ServletRequest.getHeaders(String) 获得)。它与头隐式对象非常类似。表达式 ${headerValues.name} 相当于 request.getHeaderValues(name)。

cookie 将 cookie 名称映射到单个 cookie 对象。向服务器发出的客户端请求可以获得一个或多个 cookie。表达式 ${cookie.name.value} 返回带有特定名称的第一个 cookie 值。如果请求包含多个同名的 cookie,则应该使用 ${headerValues.name} 表达式。
initParam 将上下文初始化参数名称映射到单个值(通过调用 ServletContext.getInitparameter(String name) 获得)。

 

除了上述两种类型的隐式对象之外,还有些对象允许访问多种范围的变量,如 Web 上下文、会话、请求、页面:

 

术语 定义

pageScope

将页面范围的变量名称映射到其值。例如,EL 表达式可以使用 ${pageScope.objectName} 访问一个 JSP 中页面范围的对象,还可以使用 ${pageScope.objectName.attributeName} 访问对象的属性。

requestScope

将请求范围的变量名称映射到其值。该对象允许访问请求对象的属性。例如,EL 表达式可以使用 ${requestScope.objectName} 访问一个 JSP 请求范围的对象,还可以使用 ${requestScope.objectName.attributeName} 访问对象的属性。

sessionScope

将会话范围的变量名称映射到其值。该对象允许访问会话对象的属性。例如:


$sessionScope.name}

applicationScope

将应用程序范围的变量名称映射到其值。该隐式对象允许访问应用程序范围的对象。

 

三、特别强调:

1、注意当表达式根据名称引用这些对象之一时,返回的是相应的对象而不是相应的属性。例如:即使现有的 pageContext 属性包含某些其他值,${pageContext} 也返回 PageContext 对象。

2、 注意 <%@ page isELIgnored="true" %> 表示是否禁用EL语言,TRUE表示禁止.FALSE表示不禁止.JSP2.0中默认的启用EL语言。

四、举例说明

1、例如,

< %=request.getParameter(“username”)% > 等价于 ${ param.username }

2、例如,但是下面的那句EL语言可以完成如果得到一个username为空,则不显示null,而是不显示值。

 

<%=user.getAddr( ) %> 等价于 ${user.addr}。

3、例如:

<% =request.getAttribute(“userlist”) %> 等价于$ { requestScope.userlist }

4、例如,原理如上例3。

${ sessionScope.userlist } 1

${ sessionScope.userlist } 2

${ applicationScope.userlist } 3

${ pageScope.userlist } 4

${uselist} 含义:执行顺序为4 1 2 3。

“.”后面的只是一个字符串,并不是真正的内置对象,不能调用对象。

4、例如,

<%=user.getAddr( ) %> 等价于 ${user.addr}

第一句前面的user,为一个变量。

第二句后面user,必须为在某一个范围里的属性。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326429486&siteId=291194637