一、EL表达式
1、定义及作用
EL表达式的全称是:Expression Language,是表达式语言
作用:EL表达式主要是代替jsp页面中的表达式脚本在jsp页面中进行数据的输出
因为EL表达式在输出数据时,要比jsp的表达式脚本要简洁很多
<body>
<%
request.setAttribute("key","值");
%>
表达式脚本输出key的值是:<%=request.getAttribute("key")%><br/>
EL表达式输出key的值是:${key}
</body>
EL表达式格式:${表达式}
在输出null值时,输出的是空串,jsp表达式脚本输出null值时,输出的是null字符串
2、EL表达式搜索域数据的顺序
EL表达式主要是在jsp页面中输出数据
主要是输出域对象中的数据
当四个域中都有相同的key数据时,EL表达式会按照四个域的从小到大顺序进行搜索,找到就输出
<body>
<%
//四个域中都保存了相同的key数据
pageContext.setAttribute("key","pageContext");
request.setAttribute("key","request");
session.setAttribute("key","session");
application.setAttribute("key","application");
%>
${ key }
</body>
3、EL表达式输出复杂的Bean对象
需求----输出Bean的普通属性,数组属性,List集合属性,map集合属性
<body>
<%
Person person = new Person();
person.setName("小哥哥");
person.setPhone(new String[]{"18679965896","15796905780","15975806589"});
List<String> cities = new ArrayList<String>();
cities.add("北京");
cities.add("上海");
cities.add("杭州");
person.setCities(cities);
Map<String,Object>map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
person.setMap(map);
pageContext.setAttribute("person",person);
%>
输出Person:${ person }<br>
输出Person的name属性:${ person.name }<br>
输出Person的phone数组属性值:${ person.phone[2] }<br>
输出Person的cities集合中的元素值:${person.cities}<br>
输出Person的List集合中个别元素值:${ person.cities[2] }<br>
输出Person的Map集合:${ person.map }<br>
输出Person的Map集合中某个key的值:${ person.map.key1 }<br>
</body>
4、EL表达式—运算
语法:${运算表达式},EL表达式支持如下运算符
①关系运算
==/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/{3lt}—true
gt(>): 2 > 10 / {2>10}/ 2>10/{2gt10}—false
<=/le: 5 < = 12 / {5<=12}/ 5<=12/{5le12}—true
ge(>=): 3 > = 5 / {3>=5}/ 3>=5/{3ge5}—false
②逻辑运算
&&/and(与运算):KaTeX parse error: Expected '}', got '&' at position 9: {12==12 &̲& 12<11}/{12==12 and 12<11}—false
||/or(或运算): 12 = = 12 ∣ ∣ 12 < 11 / {12==12 || 12<11}/ 12==12∣∣12<11/{12==12 or 12<11}—true
!/not(取反运算): ! t r u e / {!true}/ !true/{not true}—false
③算数运算
+:${12+18}—30
-:${18-8}—10
*:${12·12}—144
/或div: 144 / 12 / {144/12}/ 144/12/{144 div 12}—12
%或mod(取模):KaTeX parse error: Expected '}', got 'EOF' at end of input: {144%10}/{144 mod 10}—4
④empty运算
可以判断一个数据是否为空,如果为空,则输出true,不为空则输出false
以下几种情况为空:
1.职位null值时,为空
2.值为空串时,为空
3.值为Object类型数组,长度为0时
4.list集合,元素个数为0时
5.map集合,元素个数为0时
<body>
<%
// 1.职位null值时,为空
request.setAttribute("emptyNull",null);
// 2.值为空串时,为空
request.setAttribute("emptyStr","");
// 3.值为Object类型数组,长度为0时
request.setAttribute("emptyArr",new Object[]{});
// 4.list集合,元素个数为0时
List<String> list = new ArrayList<>();
request.setAttribute("emptyList",list);
// 5.map集合,元素个数为0时
Map<String,Object> map = new HashMap<>();
request.setAttribute("emptyMap",map);
%>
${empty emptyNull}<br/>
${empty emptyStr}<br/>
${empty emptyArr}<br/>
${empty emptyList}<br/>
${empty emptyMap}<br/>
</body>
⑤三元运算
表达式1 ?表达式2 : 表达式3
如果表达式1的值,返回表达式2的值,如果表达式1的值为假,返回表达式3的值
⑥“.”运算和[]中括号运算符
.点运算可以输出Bean对象中某个属性的值
[]中括号运算,可以输出有序集合中某个元素的值
且[]还可以输出map集合中key里含有特殊字符的key的值
<body>
<%
Map<String,Object> map = new HashMap<String,Object>();
map.put("aaa","aaaValue");
map.put("bbb","bbbValue");
map.put("ccc","cccValue");
request.setAttribute("map",map);
%>
${ map.aaa }
</body>
⑦EL表达式的11个隐含对象
11个隐含对象是自己定义的,可以直接使用
pageContext(PageContextImpl类型)-作用:可以获取jsp中的九大内置对象
EL获取四个特定域中的属性:
pageScope(Map<String,Object>)-作用:可以获取pageContext域中的数据
requesScope(Map<String,Object>)-作用:可以获取Request域中的数据
sessionScope(Map<String,Object>)-作用:可以获取Seesion域中的数据
application(Map<String,Object>)-作用:可以获取ServletContext域中
param(Map<String,String>):可以获取请求参数的值
paramValues(Map<String,String[]>):也可以获取请求参数的值,获取多个值时使用
<body>
输出请求参数username的值:${param.username}<br>
输出情趣参数password的值:${param.password}<br>
输出请求参数username的值:${ paramValues.username[0] }<br>
</body>
header(Map<String,String>):可以获取请求头的信息
headerValues(Map<String,String[]>):可以获取请求头的信息
输出请求头【User-Agent】的值:${header['User-Agent']}<br>
输出请求头【Connection】的值:${header.Connection}<br>
输出请求头【User-Agent】的值:${header['User-Agent'][0]}<br>
cookie(Map<String,Cookie>):可以获取当前请求的Cookie信息
获取Cookie的名称:${cookie.JSEEIONID.name}<br>
获取Cookie的值:${cookie.JSEEIONID.value}<br>
initParam(Map<String,String>):可以获取在web.xml中配置的上下文参数
输出<Context-param>username的值:${initParam.username}<br>
输出<Context-param>url的值:${initParam.url}<br>
二、JSTL标签库
是一个不断完善的开放源代码的JSP标签库
EL表达式主要是为了替换jsp中的表达式脚本,标签库是为了替换代码脚本,使整个jsp页面变得更加简洁
1、JSTL标签库的使用步骤
①先导入jstl标签库的jar包
②使用taglib指令引入标签库
2、core核心库的使用
①<c:set/>
作用:set标签可以往域中保存数据
②<c:if/>
if标签用来做if判断
③<c:choose> <c:when> <c:otherwise>标签
作用:多路判断,跟switch…case…default十分接近
choose标签开始选择判断
when标签标示每一种判断情况
test属性标示当前这种判断情况的值
这三个标签使用时需要注意:
1.标签里不能使用html注释
2.when标签的父标签一定要是choose标签
<c:choose>
<c:when test = "${requestScope.height > 190}">
<h2>小巨人</h2>
</c:when>
<c:when test = "${requestScope.height > 180}">
<h2>很高</h2>
</c:when>
<c:when test = "${requestScope.height > 170}">
<h2>还可以</h2>
</c:when>
<c:otherwise>
<h2>剩下小于170的情况</h2>
</c:otherwise>
</c:choose>
④<c:foreach/>
作用:遍历输出使用
遍历1:遍历1-10,输出
begin属性设置开始的索引
end属性设置结束的索引
var属性表示循环的变量(当前正在遍历到的数据)
<c:forEach begin = "1" end = "10" var = "i">
${ i }
</c:forEach>
遍历2:遍历Object数组
for(Object item : arr)
items:表示遍历的数据源(遍历的集合)
var:表示当前遍历到的数据
<hr>
<%
request.setAttribute("arr",new String[]{"19678995859","18668964768","19569685906"});
%>
<c:forEach items = "${ requestScope.arr }" var = "">
${ item } <br>
</c:forEach>
遍历3:遍历Map集合
<hr>
<%
Map<String,Object> map = new HashMap<String,Object>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
/*for (Map.Entry<String,Object> entry : map.entrySet()){
}*/
%>
<c:forEach items = "${ requestScope.map}" var = "entry">
<h1>${entry}</h1>
</c:forEach>
遍历4:遍历List集合----list中存放Person类,有属性:编号,用户名,密码,年龄,电话信息
<%
List<Student> studentList = new ArrayList<Student>();
for (int i = 0;i < 10;i++){
studentList.add(new Student(1,"username"+ i,"password" + i,18+i,"phone"+i));
}
request.setAttribute("stus",studentList);
%>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>电话</th>
<th>操作</th>
</tr>
</table>
<c:forEach items="${requestScope.stus}" var = "stu">
<tr>
<td>${stu.id}</td>
<td>${stu.username}</td>
<td>${stu.password}</td>
<td>${stu.age}</td>
<td>${stu.phone}</td>
<td>删除、<修改></修改></td>
</tr>
</c:forEach>