JSTL是为了替代JSP中的代码脚本。
一. 使用步骤
1.导入JSTL标签库的jar包
2.使用taglib指令引入标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
二. 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>
<%
request.setAttribute("key1", "value1");
%>
${requestScope.key1} <br/>
<!--以上可以使用jstl转化为如下-->
<!--scope表示保存到哪个域,page表示保存到pageContext域,request表示Request域,session表示Session域,application表示ServletContext域-->
<!--var设置key,value设置值-->
<c:set scope="request" var="key2" value="value2"/>
${requestScope.key2} <br/>
</body>
</html>
2. <c:if />:做if判断(只能做if判断,不可以if-else)
<%@ 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>
<!-- test属性表示判断的条件(使用EL表达式) -->
<c:if test="${12 == 12}">
12等于12
</c:if>
</body>
</html>
3. <c:choose> <c:when> <c:otherwise>:可以做多路判断(与switch-case-default类似)
<%@ 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("height",178);
%>
<!-- choose标签开始选择判断,when标签表示每一种判断情况,test表示判断的条件,otherwise表示剩下的情况 -->
<!-- 在choose-when-otherwise里不能使用html注释,只能使用jsp注释 -->
<!-- 在when的父标签必须是choose标签,如果想在otherwise里继续判断,必须先写choose标签 -->
<c:choose>
<c:when test="${requestScope.height > 190}">
很高
</c:when>
<c:when test="${requestScope.height > 180}">
高
</c:when>
<c:when test="${requestScope.height > 170}">
还行
</c:when>
<c:otherwise>
<c:choose>
<c:when test="${requestScope.height > 160}">
凑和
</c:when>
<c:when test="${requestScope.height > 150}">
矮
</c:when>
<c:otherwise>
其他情况
</c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</body>
</html>
4. <c:forEach />:遍历
①遍历普通数据
<%@ 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>
<!-- 1.遍历1到10-->
<!-- begin设置开始的索引,end设置结束的索引,var设置循环的变量名-->
<c:forEach begin="1" end="10" var="i">
${i}
</c:forEach>
<!-- 可以在forEach内设置其他内容-->
<table border="1">
<c:forEach begin="1" end="10" var="i">
<tr>
<td>第${i}行</td>
</tr>
</c:forEach>
</table>
</body>
</html>
②遍历Object数组
<%@ 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>
<!-- 2.遍历Object数组-->
<!-- items表示遍历的数据源,var表示当前遍历的数据-->
<%
request.setAttribute("names",new String[]{"张三","李四","王二"});
%>
<c:forEach items="${requestScope.names}" var="name">
${name}
</c:forEach>
</body>
</html>
③遍历Map
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ 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>
<!-- 3.遍历Map-->
<!-- items表示遍历的数据源,var表示当前遍历的数据-->
<%
Map<String,Object> map = new HashMap<>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
request.setAttribute("map",map);
%>
<c:forEach items="${requestScope.map}" var="entry">
${entry} <br/>
${entry.key} <br/>
${entry.value} <br/>
${entry.key} = ${entry.value}<br/>
</c:forEach>
</body>
</html>
④遍历List
<%@ page import="java.util.List" %>
<%@ page import="pojo.Person" %>
<%@ 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>
<!-- 4.遍历List-->
<!-- items表示遍历的数据源,var表示当前遍历的数据-->
<%
List<Person> personList = new ArrayList<>();
for(int i = 1; i <= 10; i++){
personList.add(new Person("name"+i, "password"+i, 18+i));
}
request.setAttribute("person",personList);
%>
<c:forEach items="${requestScope.person}" var="p">
${p} <br/>
</c:forEach>
<table border="1">
<tr>
<th>姓名</th>
<th>密码</th>
<th>年龄</th>
</tr>
<c:forEach items="${requestScope.person}" var="p">
<tr>
<td>${p.name}</td>
<td>${p.password}</td>
<td>${p.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
package pojo;
public class Person {
private String name;
private String password;
private Integer age;
public Person() {
}
public Person(String name, String password, Integer age) {
this.name = name;
this.password = password;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
⑤forEach标签组合使用去遍历
<%@ page import="pojo.Person" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ 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>
<!-- begin设置开始的索引,end设置结束的索引,items表示遍历的数据源,var表示当前遍历的数据-->
<!-- step表示遍历的步长值--->
<%
List<Person> personList = new ArrayList<>();
for(int i = 1; i <= 10; i++){
personList.add(new Person("name"+i, "password"+i, 18+i));
}
request.setAttribute("person",personList);
%>
<table border="1">
<tr>
<th>姓名</th>
<th>密码</th>
<th>年龄</th>
</tr>
<c:forEach begin="2" end="9" step="2" items="${requestScope.person}" var="p">
<tr>
<td>${p.name}</td>
<td>${p.password}</td>
<td>${p.age}</td>
</tr>
</c:forEach>
</table>
</body>
</html>