目录
EL表达式
作用:在jsp页面输出数据(主要是输出域对象中的数据)
一、格式
格式:$(表达式)
EL表达式在输出null值时,输出的是空串,而jsp表达式脚本是输出null字符串
二、搜索域数据的数据
按照四个域从小到大的顺序进行搜索,找得到就输出
三、表达式输出复杂Bean对象
<body>
<%
Person person=new Person();
person.setName("国哥好帅");
person.setPhones(new String[]{"18610541354","18688886666","18699998888"});
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("p",person);
%>
输出Person:${p}<br/>
输出Person的name属性:${p.name}<br/>
输出Person的phone数组属性:${p.phones[2]}<br/>
输出Person的cities集合中的元素值:${p.cities[2]}<br/>
输出Person的Map集合:${p.map.key1}<br/>
</body>
四、EL表达式中的运算
1、关系运算
2、逻辑运算
3、算术运算
4、empty运算
empty运算可以判断一个数据是否为空,如果为空则输出true,否则输出false
格式:${empty 名称}
以下几种情况为空:
4.1、值为null
4.2、值为空串
4.3、值为Object类型数组,长度为零
4.4、list集合,元素个数为零
4.5、map集合,元素个数为零
5、三元运算
6、点运算和中括号运算
点运算:可以输出Bean对象中某个属性的值
[ ]中括号运算:可以输出有序集合中某个元素的值,还可以输出map集合中key里含有特殊字符的key的值
<body>
<%
Map<String,String> map=new HashMap<String,String>();
map.put("a.a.a","aaaValue");
map.put("b+b+b","bbbValue");
map.put("c-c-c","cccValue");
request.setAttribute("f",map);
%>
${ f["a.a.a"] }<br/>
${ f["b+b+b"] }<br/>
${ f["c-c-c"] }<br/>
</body>
五、EL表达式中的11个隐含对象
EL表达式中的11个隐含对象,是EL表达式自己定义的,可以直接使用
变量 | 类型 | 作用 |
---|---|---|
pageContext | pageContextImpl | 可以获取jsp中的9大内置对象 |
pageScope | Map<String,Object> | 获取pageContext域中的数据 |
requestScope | Map<String,Object> | 获取Request域中的数据 |
sessionScope | Map<String,Object> | 获取session域中的数据 |
applicationScope | Map<String,Object> | 获取ServletContext域中的数据 |
param | Map<String,String> | 获取请求参数的值 |
paramValues | Map<String,String[]> | 获取请求参数的多个值 |
header | Map<String,String> | 获取请求头的信息 |
headerValues | Map<String,String[]> | 获取请求头多个值的信息 |
cookie | Map<String,Cookie> | 获取当前请求的Cookie信息 |
initParam | Map<String,String> | 获取在web.xml中配置的<context.param>上下文参数 |
pageScope |
requestScope |
sessionScope |
applicationScope |
的作用
<body>
<%
pageContext.setAttribute("key","value1");
request.setAttribute("key","value2");
session.setAttribute("key","value3");
application.setAttribute("key","value4");
%>
${applicationScope.key}
</body>
pageContext的作用:
获取{
1、协议
2、服务器IP
3、服务器端口
4、获取工程路径
5、获取请求方法
6、获取客户端IP地址
7、获取会话的id编号
}
<body>
<%
pageContext.setAttribute("req",request);
pageContext.setAttribute("se",session);
%>
1、协议:${req.scheme}<br/>
2、服务器IP:${req.serverName}<br/>
3、服务器端口:${req.serverPort}<br/>
4、获取工程路径:${req.contextPath}<br/>
5、获取请求方法:${req.method}<br/>
6、获取客户端IP地址:${req.remoteHost}<br/>
7、获取会话的id编号:${se.id}<br/>
</body>
其他隐含对象:
<body>
输出请求参数username的值: ${param.username}<br/>
输出请求参数password的值: ${param.password}<br/>
输出请求参数hobby 的值:${paramValues.hobby[0]}<br/>
<hr/>
输出请求头【User-Agent】的值:${header["User-Agent"]}<br/>
输出请求头[Connection]的值:${header.Connection}<br/>
输出请求头[User-Agent]的值:${headerValues["User-Agent"][0]}<br/>
<hr/>
输出Cookie的名称: ${cookie.JSESSIONID.name}<br/>
输出Cookie的值: ${cookie.JSESSIONID.value}<br/>
<hr/>
输出<Context-param>中username的值:${initParam.username}<br/>
输出<Context-param>中password的值:${initParam.password}<br/>
</body>
JSTL标签库
含义: JSP Standard Tag Library(jsp标准标签库)
JSTL是为了替换代码脚本,EL表达式是为了替换jsp中的表达式脚本
功能范围 | URL |
---|---|
核心标签库--重点 | http://java.sun.com/jsp/jstl/core |
格式化 | http://java.sun.com/jsp/jstl/fmt |
函数 | http://java.sun.com/jsp/jstl/functions |
数据库(不常用) | http://java.sun.com/jsp/jstl/sql |
XML(不常用) | http://java.sun.com/jsp/jstl/xml |
使用步骤:
1、导入jstl标签库的jar包
2、使用taglib指令引入标签库
core核心库使用
1、<c:set/>
作用:set标签可以往域中保存数据
<body>
保存之前:${requestScope.abc}<br/>
<c:set scope="request" var="abc" value="abcValue"/><br/>
保存之后:${requestScope.abc}<br/>
</body>
要点总结:
scope属性设置保存到哪个域
var属性设置key是多少
value属性设置值
2、<c:if/>
用来做if判断
<c:if test="${12==12}">
12等于12
</c:if>
要点总结:test属性表示判断的条件(使用EL表达式)
3、<c:choose><c:when><c:otherwise/>
choose表示开始选择标签
when表示 每一种判断情况
test表示当前这种判断情况的值
<%
request.setAttribute("height",192);
%>
<c:choose>
<c:when test="${requestScope.height>190}">
<h2>小巨人</h2>
</c:when>
<c:when test="${requestScope.height>170}">
<h2>很高</h2>
</c:when>
<c:otherwise>
<h2>还可以</h2>
<c:choose>
<c:when test="${requestScope.height<150}">
<h2>有点矮了</h2>
</c:when>
<c:otherwise>
<h2>还可以</h2>
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
4、<c:forEach/>
begin属性设置开始的索引
end属性设置结束的索引
var属性设置循环的变量(也是当前正在遍历的数据)
4.1、遍历数字
<body>
<c:forEach begin="1" end="10" var="i">
${i}
</c:forEach>
</body>
4.2、遍历对象数组
<%
request.setAttribute("arr",new String[]{"18610541354","18688886666","18699998888"});
%>
<c:forEach items="${requestScope.arr}" var="item">
${item}<br/>
</c:forEach>
要点总结:items表示遍历的数据源(遍历的集合)
var表示当前遍历到的数据
4.3、遍历map
<%
Map<String,Object> map=new HashMap<String,Object>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
${entry.key}=${entry.value}
</c:forEach>
4.4、遍历list集合
<%List<Person> studentList=new ArrayList<Person>();
for(int i=1;i<=10;i++){
studentList.add(new Person(i,"name"+i,"password"+i));
}
request.setAttribute("stus",studentList);
%>
<c:forEach items="${requestScope.stus}" var="stu">
${stu}
</c:forEach>
4.5、其他属性
current | 获取当前遍历的数据 |
Index | 获取遍历的索引 |
count | 获取遍历的个数 |
first | 是否是遍历的第一条数据 |
last | 是否是遍历的最后一条数据 |
begin | 获取begin属性值 |
end | 获取end属性值 |
step | 获取step属性值 |