EL表达式
1. 什么是EL表达式
EL表达式的作用是:使JSP中的java代码没有那么繁琐。
EL表达式的格式:
.
.
.
.
。
例
如
:
{....}。例如:
....。例如:{1 + 2}
在使用El表达式时,要求page指令的isELlgnored属性为false。
2. EL运算符
运算符 | 说明 | 范例 | 结果 |
---|---|---|---|
+ | 加 | ${17+5} | 22 |
- | 减 | ${17-5} | 12 |
* | 乘 | ${17*5} | 85 |
/ 或div | 除 | 17 / 5 或 {17/5}或 17/5或{17div5} | 3 |
%或mod | 取余 | KaTeX parse error: Expected '}', got 'EOF' at end of input: {17%5}或{17 mod 5} | 2 |
==或eq | 等于 | 5 = 5 或 {5=5}或 5=5或{5eq5} | true |
!=或ne | 不等于 | 5 ! = 5 或 {5!=5}或 5!=5或{5ne5} | false |
<或lt | 小于 | 3 < 5 或 {3<5}或 3<5或{3lt5} | true |
>或gt | 大于 | 3 > 5 或 {3>5}或 3>5或{3gt5} | false |
<=或le | 小于等于 | 3 < = 5 或 {3<=5}或 3<=5或{3 le 5} | true |
>=或ge | 大于等于 | 3 > 5 或 {3>5}或 3>5或{3ge5} | false |
&&或and | 并且 | t r u e 或 f a l s e 或 {true或false}或 true或false或{true and false} | false |
!或not | 非 | ! t r u e 或 {!true}或 !true或{not true} | false |
or | 或者 | ${true or false} | true |
empty | 是否为空 | e m p t y “ ” , 可 以 判 断 字 符 串 、 数 组 、 集 合 的 长 度 是 否 为 0 , 为 0 返 回 t r u e 。 e m p t y 还 可 以 与 n o t 或 ! 一 起 使 用 。 {empty “”},可以判断字符串、数组、集合的长度是否为0,为0返回true。empty还可以与not或!一起使用。 empty“”,可以判断字符串、数组、集合的长度是否为0,为0返回true。empty还可以与not或!一起使用。{not empty “”} | true |
emptynot | 非空判断 |
3. EL内置对象
在EL表达式中,无需创建就可以使用的对象称之为EL隐藏(隐含、内置)对象。在EL中一共有11个隐藏对象,它们都与Map相似。其中10个是Map,一个是 PageContext
参数隐藏对象
- param:
param是Map<String, String>类型。param对象可以用来获取参数,与request.getParameter()方法相同。
注意:在使用EL获取参数时,如果参数不存在,返回的是空字符串,而不是null。这一点与request.getParameter()方法不同。
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
out.println(username + " : " + password);
<h2>${param.username }</h2>
<h2>${param.password }</h2>
%>
- paramValues:paramValues是Map<Sting, String[]>类型,当一个参数名,对应多个参数值时可以使用它。
${paramValues.favor[0] },${paramValues.favor[1] }
- header:header是Map<String, String>类型,用来获取请求头
${header.Host }
-
headerValues:headerValues是Map<String, String[]>类型。当一个请求头信息,对应多个值时,使用该对象。
-
initfParam:initParam是Map<String, String>类型。它对应web.xml文件中的参数。
<context-param>
<param-name>person_name</param-name>
<param-value>values_name</param-value>
</context-param>
- cookie:cookie是Map<String, Cookie>类型。其中key是Cookie的名字,而值是Cookie对象本身。
response.addCookie(new Cookie("personName","personValue"));
${cookie.personName.name }:${cookie.personName.value }
域隐藏对象
使用EL表达式最为常用的就是获取域对象中保存的数据。例如:${pageScope.xxx}
,表示获取在pageContext保存数据。当然${pageScope['xxx']}
是相同的。
-
pageScope:pageScope是Map<String, Object>类型,${pageScope.xxx} 的功能相当于pageContext.getAttribute(“xxx”)。两者的区别在数据不存在时返回空字符串,而后者返回null。
-
requestScope:requestScope是Map<String, Object>类型,装载了request对象中的所有数据。
-
sessionScope:sessionScope是Map<String, Object>类型,装载了session对象中的所有数据。
-
applicationScope:applicationScope是Map<String, Object>类型,装载了application对象中的所有数据。
当EL中给出的不是隐藏域对象时,表示在四个域中查找数据。例如:${a},表示
- 在${pageScope.a}中查找,如果找到就返回。
- 在${requestScope}中查找,如果找到就返回。
- 在${sessionScope}中查找,如果找到就返回。
- 在${applicationScope}中查找,如果找到就返回,找不到就返回空字符串。
<%
Person person = new Person();
person.setName("Name");
person.setAge(19);
person.setAddress("address");
pageContext.setAttribute("person", person);
request.setAttribute("person", person);
session.setAttribute("person", person);
application.setAttribute("person", person);
%>
<%pageContext.setAttribute("name", "kaiaki"); %>
${pageScope.name }
${requestScope.person.age }
${sessionScope.person.address }
${applicationScope.person.name }
${person.age }
页面上下文隐藏对象
- pageContext:pageContext是pageContext类型。可以使用pageContext对象调用getxxx()方法,例如:pageContext.getRequest,可以${pageContext.request}。
4. JSTL
什么是JSTL
JSP标准标记库,是一个实现web应用程序中常见的通用功能的定制标记库集,这些功能包括迭代和条件判断,数据管理格式化,XML操作以及数据库的访问。
导入标签库
导入标签库需要使用taglib指令!
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>(核心库)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/fmt" %>(关于数据格式化)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/functions" %>(内部提供的函数)
out 和 set
out:
用法 | 输出 |
---|---|
<c:out value=“aaa”> | 输出aaa字符串常量 |
<c:out value="${aaa}"> | 与${aaa}相同 |
<c:out value="${aaa}" default=“xxx”> | 当{aaa}不存在时,输出xxx字符串 |
pageContext.setAttribute("pname", "pname_value");
<c:out value="aaa"></c:out><br>
<c:out value="${pname }"></c:out><br>
<c:out value="${name }" default="unkown"></c:out>
set:
用法 | 输出 |
---|---|
<c:set var=“a” value=“hello”> | 在pageContext中添加name为a,value为hello的数据 |
<c:set var=“a” value=“hello” scope=“session”> | 在session中添加name为a,value为hello的数据 |
<c:set var="pname1" value="pname1_value"></c:set>
<c:set var="pname2" value="pname2_value" scope="request"></c:set>
if 和 choose
if 标签:
test:判断条件是否满足
<c:set var="pname1" value="pname1_value"></c:set>
<c:if test="${not empty pname1 }">
<c:out value="${pname1 }"></c:out>
</c:if>
choose 标签:
choose标签对应java中的if/else if/else结构。
when标签的test为true时,会执行这个when的内容。
<c:set var="score" value="78"></c:set>
<c:choose>
<c:when test="${score < 60 }">
<c:out value="${'没及格' }"></c:out>
</c:when>
<c:when test="${score >= 60 && score < 85 }">
<c:out value="${'及格' }"></c:out>
</c:when>
<c:otherwise>
<c:out value="${'特别好' }"></c:out>
</c:otherwise>
</c:choose>
forEach(循环标签)
String[] names = {"zhangsan", "lisi", "wangwu"};
pageContext.setAttribute("ns", names);
<c:forEach var="item" items="${ns }">
<c:out value="name: ${item }"></c:out><br>
</c:forEach>