EL表达式
EL表达式,它可以帮你获取四大域对象中存储的数据。
四大域: pageContext, request, session, application.
之前: request.setAttribute(key,value) 往request中存储数据。
request.getAttribute(key); 获取request中指定key的数据。
如果在jsp中使用 <%里面%>
以后再jsp中尽量不要使用<%java脚本代码。%>
语法: ${key} 如果没有指定范围 默认从page范围逐个往下扫描。如果扫描到则不会继续扫描。
如果想指定范围扫描 ${xxxScope.key}
<html>
<head>
<title>$Title$</title>
</head>
<body>
<h1>之前写法</h1>
<%
pageContext.setAttribute("k1","pageContext内容"); //当前网页
request.setAttribute("k1","request内容"); //同一个请求有效
session.setAttribute("k1","session内容"); //同一个会话
application.setAttribute("k1","application内容"); //同一个应用。
%>
<%=pageContext.getAttribute("k1")%><br>
<%=request.getAttribute("k1")%><br>
<%=session.getAttribute("k1")%><br>
<%=application.getAttribute("k1")%><br>
<h1>使用EL表达式</h1>
<%--
如果${key} 默认从PageContext扫描,如果存在指定的key,则不会继续扫描, 如果指定的key不存在则扫描request, 扫描session, application
--%>
${pageScope.k1}<br>
${requestScope.k1}<br>
${sessionScope.k1}<br>
${applicationScope.k1}<br>
</body>
</html>
演示以下 ${}的使用
例子:
<body>
<%
request.setAttribute("user",new User("刘德华",18));
%>
<%--解析{}中的内容。 --%>
${user.name}<br>
${15>10}<br>
15>10=${15>10}<br>
</body>
</html>
jstl标签库
核心标签库
为了不在jsp中使用<%%>. 如果jsp中用的if for语句。这时可以使用jstl标签库。
如何使用标签库
1. 引入jstl标签库依赖
2.再相应网页中引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3.使用标签库.
<%--forEach:循环标签
items:需要循环集合
var: 循环时把集合中的每个元素赋值的变量名
begin: 从第一个元素开始 默认0
end:
step: 每次循环执行的步数
--%>
<c:forEach items="${users}" var="u" step="3">
<tr>
<td>${u.name}</td>
<td>${u.age}</td>
</tr>
</c:forEach>
if标签和choose标签
判断标签。没有else标签。
<%
request.setAttribute("age",17);
%>
<%--test:条件 --%>
<c:if test="${age>=18}">
成年
</c:if>
<c:if test="${age<18}">
未成年
</c:if>
<%
request.setAttribute("week",8);
%>
<c:choose>
<c:when test="${week==1}">星期一</c:when>
<c:when test="${week==2}">星期二</c:when>
<c:when test="${week==3}">星期三</c:when>
<c:when test="${week==4}">星期四</c:when>
<c:when test="${week==5}">星期五</c:when>
<c:when test="${week==6}">星期六</c:when>
<c:when test="${week==7}">星期日</c:when>
<c:otherwise>数字有误</c:otherwise>
</c:choose>
格式化标签库
1.引入格式化的标签库
<%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %>
2.使用格式化标签
综合案例
展示所有的学生信息
关于student表的操作---crud操作并使用junit测试。
增加 删除 修改 根据id查询 查询所有