jsp表达式语言
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:
表达式语言(EL)已添加到JSP 2.0规范中。 EL的目的是产生无脚本的JSP页面。 JSP中EL的语法如下:
${expr}
Here expr is a valid EL expression. An expression can be mixed with static text/values and can also be combined with other expressions to form larger expression.
这里expr是有效的EL表达式。 表达式可以与静态文本/值混合,也可以与其他表达式组合以形成更大的表达式。
EL表达式如何使用? (How EL expression is used?)
EL expression can be used in two ways in a JSP page
在JSP页面中可以两种方式使用EL表达式
As attribute values in standard and custom tags. Example:
作为标准和自定义标签中的属性值。 例:
<jsp:include page="${location}">
Where location variable is separately defines in the jsp page.
在jsp页面中分别定义位置变量的位置。
Expressions can also be used in
jsp:setProperty
to set a properties value, using other bean properties like : If we have a bean named Square with properties length, breadth and area.也可以在
jsp:setProperty
使用表达式来设置属性值,并使用其他bean属性,例如:如果我们有一个名为Square的bean,其属性为length,breadth和area。<jsp:setProperty name="square" property="area" value="${square.length*square.breadth}" />
To output in HTML tag:
要以HTML标签输出:
<h1>Welcome ${name}</h1>
To deactivate the evaluation of EL expressions, we specify the isELIgnored
attribute of the page directive as below:
要停用EL表达式的求值,我们指定page指令的isELIgnored
属性,如下所示:
<%@ page isELIgnored ="true|false" %>
JSP EL隐式对象 (JSP EL Implicit Objects)
Following are the implicit objects in EL :
以下是EL中的隐式对象:
Implicit Object | Description |
---|---|
pageContext | It represents the PageContext object. |
pageScope | It is used to access the value of any variable which is set in the Page scope |
requestScope | It is used to access the value of any variable which is set in the Request scope. |
sessionScope | It is used to access the value of any variable which is set in the Session scope |
applicationScope | It is used to access the value of any variable which is set in the Application scope |
param | Map a request parameter name to a single value |
paramValues | Map a request parameter name to corresponding array of string values. |
header | Map containing header names and single string values. |
headerValues | Map containing header names to corresponding array of string values. |
cookie | Map containing cookie names and single string values. |
隐式对象 | 描述 |
---|---|
pageContext | 它代表PageContext对象。 |
pageScope | 它用于访问在页面范围中设置的任何变量的值 |
requestScope | 它用于访问在“请求”范围中设置的任何变量的值。 |
sessionScope | 它用于访问在会话范围内设置的任何变量的值 |
应用范围 | 它用于访问在应用程序范围内设置的任何变量的值 |
参数 | 将请求参数名称映射到单个值 |
参数值 | 将请求参数名称映射到相应的字符串值数组。 |
标头 | 包含标题名称和单个字符串值的映射。 |
headerValues | 将包含标题名称的映射映射到相应的字符串值数组。 |
曲奇饼 | 包含Cookie名称和单个字符串值的映射。 |
JSP EL的示例 (Example of JSP EL)
Let's take a simple example for understanding the JSP expression language,
让我们以一个简单的示例来了解JSP表达式语言,
index.jsp
index.jsp
<form method="POST" action="welcome.jsp">
Name <input type="text" name="user" >
<input type="submit" value="Submit">
</form>
welcome.jsp
welcome.jsp
<html>
<head>
<title>Welcome Page</title>
</head>
<body>
<h1>Welcome ${param.name}</h1>
</body>
</html>
EL中可用的算术运算 (Arithmetic Operations available in EL)
Following are the arithmetic operators avilable in EL:
以下是EL中适用的算术运算符:
Arithmetic Operation | Operator |
---|---|
Addition | + |
Substraction | - |
Multiplication | * |
Division | / and div |
Remainder | % and mod |
算术运算 | 操作员 |
---|---|
加成 | + |
减法 | - |
乘法 | * |
师 | / and div |
余 | % and mod |
EL中可用的逻辑运算符和关系运算符 (Logical and Relational Operators available in EL)
Following are the logical operator and comparators avilable in EL:
以下是适用于EL的逻辑运算符和比较器:
Logical and Relational Operator | Operator |
---|---|
Equals | == and eq |
Not equals | != and ne |
Less Than | < and lt |
Greater Than | > and gt |
Greater Than or Equal | >= and ge |
Less Than or Equal | <= and le |
and | && and and |
or | || and or |
not | ! and not |
逻辑和关系运算符 | 操作员 |
---|---|
等于 | == and eq |
不等于 | != and ne |
少于 | < and lt |
比...更棒 | > and gt |
大于或等于 | >= and ge |
小于或等于 | <= and le |
和 | && and and |
要么 | || and or |
不 | ! and not |
翻译自: https://www.studytonight.com/jsp/expression-language.php
jsp表达式语言