进入官网下载:http://tomcat.apache.org/taglibs/standard/
点击download:
找到如下两个包下载:
在eclipse中项目名称是右击,选择构建路径,进入后点击Add External JARs:
找到下载地址,并将其两个jar包导入:
在jsp文件中加入以下固定代码语句:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
如果出现Unknown错误,加入以下语句:
<%@ page isELIgnored="false" %>
如果出现以下错误:
解决方案为在Tomcat目录下的lib文件夹中加入jstl包:
重启服务器
<c:set>标签
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var = "name" value = "张三" scope = "application"></c:set>
<c:set var = "number" value = "202012" scope = "session" ></c:set>
<c:set var = "age" value = "20" scope = "request" ></c:set>
<c:set var = "sex" value = "男" scope = "page"></c:set>
<%--- scope的值为page而不是是pageContext --%>
姓名为:${applicationScope.name}<br/>
学号为:${sessionScope.number}<br/>
年龄为:${requestScope.age}<br/>
性别为:${pageScope.sex }<br/>
<%--配合EL表达式案例 --%>
<!-- 学生十年后的年龄 -->
<c:set var = "age" value = "${requestScope.age+10}" scope = "request" ></c:set>
十年后的年龄:${requestScope.age}
<c:if>标签
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var = "age" value = "20" scope = "request" ></c:set>
<c:if test = "${requestScope.age ge 20}">
<h1>你的年龄大于等于20岁</h1>
</c:if>
<c:if test = "${requestScope.age le 19}">
<h1>你的年龄小于20岁</h1>
</c:if>
<c:choose>标签
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var = "age" value = "18" scope = "request" ></c:set>
<c:choose>
<c:when test ="${request.age ge 30}">
<h1>年龄大于等于30岁</h1>
</c:when>
<c:when test = "${request.age ge 20}">
<h1>年龄大于等于20岁小于30岁</h1>
</c:when>
<c:when test = "${request.age lt 10}">
<h1>年龄小于10岁</h1>
</c:when>
<c:otherwise>
<h1>年龄大于等于10岁小于20岁</h1>
</c:otherwise>
</c:choose>
<c:forEach>标签
第一种用法:
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var = "age" value = "18" scope = "request" ></c:set>
<select>
<c:forEach var = "i" begin = "1" end = "10" step = "1">
<option >第${pageScope.i}页</option>
</c:forEach>
</select>
第二种配合javaBena和Servlet使用:
package com.wu.javaBeanDemo;
public class Student {
private String name ;
private String id;
private String course;
public Student(String name,String id,String course) {
this.name = name;
this.id = id;
this.course = course;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the course
*/
public String getCourse() {
return course;
}
/**
* @param course the course to set
*/
public void setCourse(String course) {
this.course = course;
}
}
package com.wu.javaServletDemo;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.wu.javaBeanDemo.Student;
@WebServlet(name = "forEachTest" , urlPatterns = "/forEach") // 确定客户端请求的URL为forEach
public class MyServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
// 生成数据
Student stu1 = new Student("张三","2020120901","一班");
Student stu2 = new Student("赵四","2020120902","二班");
Student stu3 = new Student("王五","2020120903","三班");
ArrayList<Student> listStudent = new ArrayList<Student>();
listStudent.add(stu1);
listStudent.add(stu2);
listStudent.add(stu3);
request.setAttribute("studentList", listStudent); // 将数据集加入request中
// 转发到其它界面处理
request.getRequestDispatcher("./index.jsp").forward(request,response);
}
}
index.jsp文件:
**<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page import = "java.util.ArrayList" %>
<%@ page import = "com.wu.javaBeanDemo.Student" %>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- 通过java代码实现 --%>
<table border = "1">
<tr>
<td>姓名</td>
<td>学号</td>
<td>班级</td>
</tr>
<%
ArrayList<Student> students = (ArrayList<Student>) request.getAttribute("studentList");
for(Student s : students){
%>
<tr>
<td><%=s.getName() %></td>
<td><%=s.getId() %></td>
<td><%=s.getCourse() %></td>
</tr>
<%}%>
</table>
<hr/>
<%-- 通过EL和JSTL实现 --%>
<table border = "1">
<tr>
<td>姓名</td>
<td>学号</td>
<td>班级</td>
</tr>
<c:forEach items = "${requestScope.studentList}" var = "s">
<tr>
<td>${pageScope.s.name}</td>
<td>${pageScope.s.id}</td>
<td>${pageScope.s.course}</td>
</tr>
</c:forEach>
</table>**
map集合方式
在MyServlet中添加以下代码:
Map mapStudent = new HashMap();
mapStudent.put("一班同学", stu1);
mapStudent.put("二班同学",stu2);
mapStudent.put("三班同学",stu3);
request.setAttribute("studentMap",mapStudent); // 将map数据集存放到request中
在index.jsp文件中添加以下代码:
<%-- 遍历map集合 --%>
<table border = "1" cellspacing = "0">
<tr>
<td>姓名</td>
<td>学号</td>
<td>班级</td>
</tr>
<%-- 每次遍历map时,都是得到一个键值对 --%>
<!--
循环变量.key 获得键值对的关键字
循环变量.value 获得键值对的内容
-->
<c:forEach items = "${requestScope.studentMap}" var = "key_value">
<tr>
<td>${pageScope.key_value.value.name}</td>
<td>${pageScope.key_value.value.id }</td>
<td>${pageScope.key_value.value.course}</td>
</tr>
</c:forEach>
</table>
<c:remove>标签
<%@ page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ page isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%-- 生成数据 --%>
<c:set var = "name" value = "张三" scope = "session"></c:set>
姓名:${sessionScope.name}
<%-- 移除变量 --%>
<c:remove var = "name" scope = "session"></c:remove>
姓名:${sessionScope.name}