JSTL
JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。
1、引入jstl.jar和standard.jar包在lib目录;
2、在JSP页面中引入标签库;
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
3、JSTL的分类:
- core:核心类库 ★
- fmt:格式化|国际化
- xml:过时了
- sql:过时了
- 函数库:很少使用
core
- c:if
- c:forEach
if标签
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--引入JSTL标签的核心库--%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("key", null);
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(100);
request.setAttribute("value", arrayList);
%>
<c:if test="${empty requestScope.key}">
${requestScope.key}
当用EL表达式取域中的属性时,当属性值为null时,在页面不显示。
</c:if>
<br>
<c:if test="${not empty requestScope.value}">
${requestScope.value[0]}
</c:if>
</body>
</html>
forEach标签
<%@ page import="java.util.ArrayList" %>
<%@ page import="org.westos.pojo.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
int[] arr = new int[]{10, 20, 30, 40, 50};
request.setAttribute("arr", arr);
%>
<%--
新式for循环,items:容器,var:当前遍历的元素
v.count:元素编号,从1开始
v.index:元素索引
v.current:当前元素的值
--%>
<c:forEach items="${arr}" var="ele" varStatus="v">
${ele}-${v.count}-${v.index}-${v.current}
</c:forEach>
<hr>
<%
ArrayList<User> users = new ArrayList<>();
users.add(new User("张三", 23));
users.add(new User("李四", 24));
request.setAttribute("users", users);
%>
<c:forEach items="${users}" var="ele" varStatus="v">
${v.index}-${ele.username}-${ele.age}
</c:forEach>
<hr>
<%
HashMap<String, User> hashMap = new HashMap<>();
hashMap.put("zs", new User("张三", 23));
hashMap.put("ls", new User("李四", 24));
request.setAttribute("map", hashMap);
%>
<%--双列集合的var是Map集合的键值对Entry对象--%>
<c:forEach items="${map}" var="ele">
${ele.key}-${ele.value.username}-${ele.value.age}
</c:forEach>
</body>
</html>
set标签、out标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:set var="username" value="john" scope="request"/>
<%--保存数据到域中,默认保存到pageContext域--%>
<c:out value="${requestScope.username}" default="<h3>标题三</h3>"/>
</body>
</html>
choose标签、when标签、otherwise标签
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:set var="score" scope="request" value="57"/>
<c:choose>
<c:when test="${requestScope.score >= 90 && requestScope.score <= 100}">
优秀
</c:when>
<c:when test="${requestScope.score >= 80 && requestScope.score < 90}">
良好
</c:when>
<c:when test="${requestScope.score >= 70 && requestScope.score < 80}">
一般
</c:when>
<c:when test="${requestScope.score >= 60 && requestScope.score < 70}">
及格
</c:when>
<c:otherwise>
不及格
</c:otherwise>
</c:choose>
</body>
</html>