1、EL表达式
1.1、什么是 EL表达式,EL表达式的作用?
EL 表达式的全称是:Expression Language.是表达式语言。
EL 表达式的作用:EL表达式主要是代替 jsp 页面中的 表达式脚本 在 jsp 页面中进行数据的输出。
因为EL表达式在输出数据的时候,要比 jsp 的表达式脚本要简洁很多。
<body>
<%
request.setAttribute("key","value");
%>
表达式脚本输出 key的值是:<%=request.getAttribute("key1") == null?"":request.getAttribute("key1")%>
<br/>
EL 表达式输出key的值是:${key1}
</body>
EL 表达式的格式是:${表达式}
EL 表达式在输出 null 值的时候,输出的是空串。jsp 表达式脚本 输出 null值得时候,输出得是 null 字符串。
1.1、 表达式搜索域数据得顺序
EL 表达式主要是在 jsp 页面中输出数据
主要是输出域对象中的数据
当四个域都有相同的 key 数据的时候,EL 表达式会按照四个域的从小到大的顺序去进行搜索,找到就输出
1.2、表达式输出复杂的Bean对象
public class Person {
private String name;
private String[] phone;
private List<String> cities;
private Map<String,Object> map;
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
Person person = new Person();
person.setName("bbc");
person.setPhone(new String[]{"12321312","123123123","12312313"});
List<String> list = new ArrayList<String>();
list.add("上海");
list.add("北京");
list.add("广州");
person.setCities(list);
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 :${person.name}<br/>
${person.phone[0]}<br/>
${person.cities}<br/>
${person.map}
</body>
</html>
运行结果
2、EL 表达式——运算
2.1、关系运算
2.2、逻辑运算
2.3、算术运算
2.4、empty 运算
2.5、"."点运算 和 [] 中括号运算符
. 点运算,可以输出 Bean 对象 中某个属性的值。
[] 中括号运算,可以输出有序集合中某个元素的值。
并且[]中括号运算,还可以输出 map 集合中 key 里含有特殊字符的 key 的值。
3、 EL 表达式的 11 个隐含对象
3.1、EL 获取四个特定域中的属性
3.2、pageCpntext 对象的使用
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
request.getscheme() 它可以获取请求的协议
request.getServerName() 获取请求的服务器 ip 或 域名
request.getServerPort() 获取请求的服务器端口号
request.getContextPath() 获取当前工程路径
request.getMethod() 获取请求方法
request.getRemoteHost() 获取客户端ip地址
request.getSession().getId() 获取会话的ip编号
--%>
<%pageContext.setAttribute("req",request);%>
<%=request.getSession().getId()%><br/>
1.协议:${pageContext.request.scheme}<br/>
2.服务器ip:${pageContext.request.serverName}<br/>
3.服务器端口:${pageContext.request.serverPort}<br/>
4.获取工程路径:${pageContext.request.contextPath}<br/>
5.获取请求方法:${pageContext.request.method}<br/>
6.获取客户端ip地址:${pageContext.request.remoteHost}<br/>
7.获取会话的ip编号:${pageContext.session.id}<br/>
</body>
</html>
运行结果
3.3、EL表达式其他隐含对象的使用
param Map<String,Object> 它可以获取请求参数的值
paramValues Map<String,Object> 它可以获取请求参数的值,获取多个值的时候使用
输出请求参数 username=${param.username}<br/>
输出请求参数 password=${param.password}<br/>
输出请求参数 username=${paramValues.username[0]}<br/>
输出请求参数 hobby=${paramValues.hobby[0]}<br/>
输出请求参数 hobby=${paramValues.hobby[1]}<br/>
header Map<String,Object> 它可以获取请求头的信息
headerValues Map<String,Object> 它可以获取请求头的信息,获取多个值的情况
输出请求头【User-Agent】=${header["User-Agent"]}<br/>
输出请求头【Connection】=${header.Connection}<br/>
输出请求头【User-Agent】=${headerValues["User-Agent"][0]}<br/>
cookie Map<String,Cookie> 它可以获取当前请求的 Cookie 信息
获取Cookie的名称=${cookie.JSESSIONID.name}<br/>
获取Cookie的值=${cookie.JSESSIONID.value}<br/>
initParam Map<String,String> 它可以获取在 web.xml 中配置的 上下文参数
输出<Context-param>username的值=${initParam.username}
输出<Context-param>url的值=${initParam.url}
4、JSTL 标签库的使用步骤
1、先导入 jstl 标签库的 jar 包。
2、使用 taglib 指令引入标签库
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
4.1、core 核心库使用
1、<c:set />
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
<c:set />
作用:set标签可以往域里保存数据
域对象.setAttribute(key,value);
scope 属性设置保存到哪个域
page 表示 pageContext 域(默认值)
request 表示 Request域
session 表示 session 域
application 表示 ServletContext 域
var 属性设置 key 是多少
value 多少
--%>
保存之前${requestScope}
<c:set scope="request" var="abc" value="abcvalue"/>
保存之后${requestScope}
</body>
</html>
2、<c:if />
<%--
<c:if />
if标签用来做if判断
test 属性表示判断的条件(使用EL表达式输出)
--%>
<c:if test="${ 12 == 12}">
<h1>12等于12</h1>
</c:if>
3、<c:choose><c:when><c:otherwise>标签
<%--
<c:choose><c:when><c:otherwise>标签
作用:多路判断。跟swith ... case ...default 非常接近
choose 标签开始选择判断
when标签表示每一种判断情况
test 属性表示当前这种判断情况的值
otherwise 标签表示剩下的情况
<c:choose><c:when><c:otherwise>标签 使用时需要注意的点:
1、标签里不能使用 html 注释,要使用jsp注释
2、when 标签的父标签一定要是 choose 标签
--%>
<%
request.setAttribute("height","168");
%>
<c:choose>
<c:when test="${requestScope.height > 190}">
<h1>小巨人</h1>
</c:when>
<c:when test="${requestScope.height > 180}">
<h1>不错</h1>
</c:when>
<c:otherwise>
<h2>太挨了</h2>
</c:otherwise>
</c:choose>
4、<c:forEach />
1.遍历 1 到 10,输出
<%--
1.遍历 1 到 10,输出
begin 属性设置开始的索引
end 属性设置结束的索引
var 属性表示循环的遍历(也是当前正在遍历的数据)
for(int i = 1;i<10;i++)
--%>
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第${ i }行</td>
</tr>
</c:forEach>
2.遍历 Object 数组
<%--
2.遍历 Object 数组
for(Object item : arr)
items 表示遍历的数据源(遍历的集合)
var 表示当前遍历的数据
--%>
<%
request.setAttribute("arr",new String[]{"123123","123213","34223423"});
%>
<c:forEach items="${requestScope.arr}" var="item">
${item}<br>
</c:forEach>
3.遍历 Map 集合
<%
Map<String,Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
// for (Map.Entry<String,Object> entry :entrySet()){}
request.setAttribute("map",map);
%>
<c:forEach items="${ requestScope.map}" var="entry">
<h1>${entry.key} = ${entry.value}</h1>
</c:forEach>
4.遍历List集合—list中存放 Student类,有属性:编号,用户名,密码,年龄,电话信息
Student
public class Student {
private Integer id;
private String username;
private String password;
private Integer age;
private String phone;
}
<%-- 4.遍历List集合---list中存放 Student类,有属性:编号,用户名,密码,年龄,电话信息--%>
<%
List<Student> studentList = new ArrayList<Student>();
for (int i = 1; i <= 10 ; i++) {
studentList.add(new Student(i,"username"+i,"pass"+i,18+i,"phone"+i));
}
request.setAttribute("stus",studentList);
%>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>电话</th>
<th>删除、修改</th>
</tr>
<c:forEach items="${requestScope.stus}" var="stu">
<tr>
<th>${stu.id}</th>
<th>${stu.username}</th>
<th>${stu.password}</th>
<th>${stu.age}</th>
<th>${stu.phone}</th>
<th>删除、修改</th>
</tr>
</c:forEach>
</table>
5、源码分析
笔记来自尚硅谷的JavaWeb课程
https://www.bilibili.com/video/BV1Y7411K7zz?p=212&spm_id_from=pageDriver