标签库的概念
JSTL标签库,全称jsp标准标签库,标签库主要是为了替代代码脚本,这样使得jsp页面变得更加简洁。
JSTL由五个不同功能的标签库组成
在jsp标签库中使用taglib指令引入标签库
JSTL标签库的使用步骤
- 先导入jstl标签库的jar包
- 使用taglib指令引入标签库
core核心库使用
<c:set/>
作用:set标签可以往域中保存数据
<%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: x2773
Date: 2021/11/18
Time: 16:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
scope属性:保存到哪个域(默认page)
var属性:key是多少
value属性:value是多少
--%>
保存之前:${requestScope.abc}<br>
<a:set scope="request" var="abc" value="abcValue"/><br>
保存之后:${requestScope.abc}
</body>
</html>
<c:if/>
作用:if标签用来做if判断
<body>
<%--
test属性:表示判断的条件(使用EL表达式输出)
--%>
<a:if test="${12==12}">
<h1>12等于12</h1>
</a:if>
</body>
<c:choose/><c:when/><c:otherwise/>标签
作用:多路判断,跟swich...case...default 非常像
<body>
<a:set scope="request" var="height" value="178"/>
<%--
choose标签表示开始选择判断
when标签表示每一种判断情况
test属性表示当前这种判断情况的值
注意的点:不能使用html注释,要使用jsp注释
when标签的父级必须是choose标签,不管你怎么套
--%>
<a:choose>
<a:when test="${requestScope.height > 190}">
<h2>小巨人</h2>
</a:when>
<a:when test="${requestScope.height > 180}">
<h2>很高</h2>
</a:when>
<a:when test="${requestScope.height > 170}">
<h2>还可以</h2>
</a:when>
<a:otherwise>
<h2>小于170的情况</h2>
</a:otherwise>
</a:choose>
</body>
<a:forEach/>
作用:遍历输出使用
- 遍历1到10,输出
<body> <%-- begin属性:设置开始的索引 end属性:设置结束的索引 var属性:表示遍历循环的变量 --%> <table> <tr> <a:forEach begin="1" end="10" var="i"> <td>第${i}列</td> </a:forEach> </tr> </table> </body>
- 遍历Object数组
<body> <%-- for(Object item:arr){} items属性: 表示遍历的数据源 --%> <% request.setAttribute("arr",new String[]{"12214312","132425tert4","2134reefe2"}); %> <a:forEach items="${requestScope.arr}" var="item"> ${item} </a:forEach> </body>
- 遍历List集合
<%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="java.util.List" %> <%@ page import="com.atguigu.pojo.Student" %> <%@ page import="java.util.ArrayList" %><%-- Created by IntelliJ IDEA. User: x2773 Date: 2021/11/19 Time: 11:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style> table{ width: 500px; border: 1px solid red; border-collapse: collapse; } tr,td{ border: 1px solid red; } </style> </head> <body> <% List<Student> list = new ArrayList<>(); for(int i = 0;i<10;i++){ list.add(new Student(i,"student"+i,"password"+i,18+i,"phone"+i)); } request.setAttribute("stus",list); %> <table> <tr> <th>编号</th> <th>用户名</th> <th>密码</th> <th>年龄</th> <th>电话</th> <th>操作</th> </tr> <a:forEach items="${requestScope.stus}" var="stu"> <tr> <td>${stu.id}</td> <td>${stu.name}</td> <td>${stu.password}</td> <td>${stu.age}</td> <td>${stu.phone}</td> <td><a>删除</a></td> </tr> </a:forEach> </table> </body> </html>
- 遍历Map集合
<body> <%-- for(Object item:arr){} items属性: 表示遍历的数据源 var属性:表示遍历的数据源的接收变量 --%> <% 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()){ // // } request.setAttribute("map",map); %> <a:forEach items="${requestScope.map}" var="entry"> <h1>${entry.key} = ${entry.value}</h1> </a:forEach> </body>
forEach标签所有属性组合使用介绍
<%@ taglib prefix="a" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="com.atguigu.pojo.Student" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: x2773
Date: 2021/11/19
Time: 11:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
table{
width: 500px;
border: 1px solid red;
border-collapse: collapse;
}
tr,td{
border: 1px solid red;
}
</style>
</head>
<body>
<%
List<Student> list = new ArrayList<>();
for(int i = 0;i<10;i++){
list.add(new Student(i,"student"+i,"password"+i,18+i,"phone"+i));
}
request.setAttribute("stus",list);
%>
<table>
<tr>
<th>编号</th>
<th>用户名</th>
<th>密码</th>
<th>年龄</th>
<th>电话</th>
<th>操作</th>
</tr>
<%--
items属性:表示遍历的集合
var属性:表示遍历到的数据
begin属性:表示遍历的开始索引值
end属性:表示遍历的结束索引值
step属性:表示遍历的步长值
for(int i = 0;i<10;i+=2){}//i+=2步长值为2
vaStatus属性:表示当前遍历到的数据的状态
--%>
<a:forEach begin="0" end="10" step="1" varStatus="status" items="${requestScope.stus}" var="stu">
<tr>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>${stu.password}</td>
<td>${stu.age}</td>
<td>${stu.phone}</td>
<td>${status.current}</td>
</tr>
</a:forEach>
</table>
</body>
</html>