文章目录
JSTL标签库
JSTL 标签库全称是指 JSP Standard Tag Library
JSP 标准标签库。是一个不断完善的开放源代码的 JSP 标签库。
EL 表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个 jsp 页面变得更加简洁。
JSTL是由五个不同功能的标签库组成
功能范围 | URI | 前缀 |
---|---|---|
核心标签库(重点) | http://java.sun.com/jsp/jstl/core | c |
格式化 | http://java.sun.com/jsp/jstl/fmt | fmt |
函数 | http://java.sun.com/jsp/jstl/functions | fn |
数据库(不使用) | http://java.sun.com/jsp/jstl/sql | sql |
XML(不使用) | http://java.sun.com/jsp/jstl/xml | x |
使用步骤
先导入JSTL标签库的jar包
jar包下载链接:
- taglibs-standard-impl-1.2.1.jar https://nowjava.com/jar/detail/m03857727/taglibs-standard-impl-1.2.1.jar.html
- taglibs-standard-spec-1.2.1.jar https://nowjava.com/jar/detail/m03857751/taglibs-standard-spec-1.2.1.jar.html
使用taglib指令引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
core核心库使用
<c:set/>(使用较少)
作用:set标签可以往域中保存数据
域对象.setAttribute(key,value);
scope 属性设置保存到哪个域
page 表示PageContext域(默认值)
request 表示Request域
session 表示Session域
application 表示 ServletContext 域
var 属性设置key value 属性设置值
代码示例
<%@ 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>
保存之前:${ sessionScope.abc}<br/>
<c:set scope="session" var="abc" value="ABC"/>
保存之后:${sessionScope.abc}<br/>
</body>
</html>
<c:if/>
代码演示
<%@ 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:if test="${3 > 4}">
${"You like me"}
</c:if>
<c:if test="${true}">
${"I like you"}
</c:if>
</body>
</html>
<c:choosee> <c:when> <c:otherwise>标签
作用:多路判断。跟 switch … case … default 类似
choose 标签开始选择判断
when 标签表示每一种判断情况
test 属性表示当前这种判断情况的值
otherwise 标签表示剩下的情况
使用时需注意:
-
标签里不能使用 html 注释,要使用 jsp 注释
-
when 标签的父标签一定要是 choose 标签
(在when或otherwise之间加多重判断时,一定要将choose写上,否则会报错)
代码演示
<%@ 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>
<%request.setAttribute("weight",82);%>
<c:choose>
<c:when test="${requestScope.weight >100}">
该减肥了,管住嘴,迈开腿
</c:when>
<c:when test="${requestScope.weight > 80}">
适量运动
</c:when>
<c:when test="${requestScope.weight > 60}">
继续保持
</c:when>
<c:otherwise>
多吃点,瘦成猴了
</c:otherwise>
</c:choose>
</body>
</html>
<c:forEach/>
作用:用于遍历输出。
begin 属性设置开始的索引
end 属性设置结束的索引
var 属性表示循环的变量(也是当前正在遍历到的数据)
step 表示步长值(默认是1)
代码演示(遍历输出1~10)
<%@ 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>
<table border="2px">
<c:forEach begin="1" end="10" var="i">
<tr>
<td>${i}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
代码演示(用增强for循环遍历输出电话号码)
items 表示遍历的数据源(遍历的集合)
var 表示当前遍历到的数据
<%@ 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>
<%
request.setAttribute("phones",new String[]{"18276639182","18948362719","13722819283","17382937659"});
%>
<table border="2px">
<c:forEach items="${requestScope.phones}" var="phone">
<tr>
<td>${phone}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
代码演示(遍历Map集合)
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ 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>
<%
Map<String, String> map = new HashMap<String, String>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
<table border="2px">
<td>${"Map"}</td>
<td>${"Key"}</td>
<td>${"Value"}</td>
<c:forEach items="${requestScope.map}" var="entry">
<tr>
<td>${entry}</td>
<td>${entry.key}</td>
<td>${entry.value}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
代码演示(遍历List集合:学生信息)
Student.java
package code;
/**
* @BelongsProject: JavaWeb
* @BelongsPackage: Student
* @Author: HeXin
* @CreateTime: 2023/2/2 21:13
* @Description:Student类
* @Version: 1.0
*/
public class Student {
private Integer id;
private String name;
private String email;
private String phone;
public Integer getId () {
return id;
}
public void setId (Integer id) {
this.id = id;
}
public String getName () {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getEmail () {
return email;
}
public void setEmail (String email) {
this.email = email;
}
public String getPhone () {
return phone;
}
public void setPhone (String phone) {
this.phone = phone;
}
public Student (Integer id, String name, String email, String phone) {
this.id = id;
this.name = name;
this.email = email;
this.phone = phone;
}
public Student () {
}
@Override
public String toString () {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
showStudent.jsp
<%@ page import="code.Student" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ 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>
<%
List<Student> list = new ArrayList<>();
request.setAttribute("student",list);
list.add(new Student(1001,"小明","xiaoming@136.com","15782916433"));
list.add(new Student(1029,"小红","xiaohong@128.com","18692705516"));
list.add(new Student(1062,"李华","lihua@sina.com","13782907615"));
list.add(new Student(1247,"张三","zhangsan@136.com","17692865141"));
list.add(new Student(1173,"佚名","yiming@qq.com","13897265514"));
request.setAttribute("students",list);
%>
<table border="2px"cellspacing="0" cellpadding="0" align="center" style="align-content: center">
<th>${"学号"}</th>
<th>${"姓名"}</th>
<th>${"邮箱"}</th>
<th>${"联系电话"}</th>
<c:forEach items="${requestScope.students}" var="student">
<tr>
<th>${student.id}</th>
<th>${student.name}</th>
<th>${student.email}</th>
<th>${student.phone}</th>
</tr>
</c:forEach>
</table>
</body>
</html>