目录:
(1)set标签使用1
(2)set标签和EL的表达式联合使用
(3)if标签
(4)choose标签 相当于多个判断语句
(5)forEach循环
(6)forEach遍历List集合
(7)forEach遍历Map集合
JSTL:披着标签的java工具类,java命令变成标签的形式,来满足需求,不让java命令落在jsp上
为了jsp页面中看不见java代码,替换原来在jsp页面中的<% java代码%>,一个标签相当于一个java命令
产生你原因:
Java命令不能落在jsp上面去,所以把java命令披上一个标签,封装成一个标签使用
另一个是EL表达式功能过于弱小
创建04Model导入响应的jar包:
(1)set标签使用1
index.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 7:11
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
set scope="application" var="sid" value="10"> set>set scope="session" var="sname" value="mike"> set>set scope="request" var="sage" value="26"> set>set scope="page" var="home" value="上海"> set>-
- 学员编号:${applicationScope.sid}
- 学员姓名:${sessionScope.sname}
- 学员年龄:${requestScope.sage}
- 学员籍贯:${pageScope.home}
(2)set标签和EL的表达式联合使用
index_2.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 8:19
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <%--设置用户今年的年龄--%>
set scope="request" var="age" value="20"> set>-
- <%--设置用户两年之后的年龄
- 执行顺序,限制性EL表达式,再执行JSTL,EL表达式先从request客户端读取数据+2,
- 然后并没有写到响应体中,而是写到value属性中,然后再执行set标签,
- 把request作用域中的20更新为22
- --%>
set scope="request" var="age" value="${requestScope.age+2}"> set>-
- 两年之后的年龄:${age}
(3)if标签
index_3.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 8:19
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <c:set scope="request" var="age" value="23">c:set>
-
-
- <c:if test="${sessionScope.age ge 18}">
- <font color="red">欢迎光临font>
- c:if>
-
- <c:if test="${sessionScope.age lt 18}">
- <font color="red">过两年再来font>
- c:if>
在运行项目的时候报了500:
JSTL标签库报500错误解决方案
原因分析
Tomcat缺少jstl-api-1.2.jar和standard-1.1.2.jar和这两个jar包。
解决方案
将jstl-api-1.2.jar和standard-1.1.2.jar这两个包放入到D:\Program\apache-tomcat-8.0.53\lib目录下
加入之后正常运行:
(4)choose标签 相当于多个判断语句
index_4.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 9:18
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <c:set scope="page" var="salary" value="15000"></c:set>
- <c:choose>
- <c:when test="${salary ge 50000}">高工资</c:when>
- <c:when test="${salary ge 20000}">正常工资</c:when>
- <c:when test="${salary ge 10000}">低工资</c:when>
- <c:otherwise>不正常工资</c:otherwise>
- </c:choose>
(5)forEach循环
index_5.jsp:
- <%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 9:42
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <select>
- <%
- for (int i=1;i<=5;i++){
- %>
- <option>第<%=i%>页option>
- <%
- }
- %>
- select>
-
- <hr>
- JSTL语法替换上方的java语句<br>
- <select>
- <c:forEach var="i" begin="1" end="5" step="1">
- <option>第${pageScope.i}页option>
- c:forEach>
- select>
(6)forEach遍历List集合
Student类:
- package com.bjpowernode.model;
-
- public class Student {
- private Integer sid;
- private String sname;
-
- public Student(Integer sid, String sname) {
- this.sid = sid;
- this.sname = sname;
- }
-
- public Integer getSid() {
- return sid;
- }
-
- public void setSid(Integer sid) {
- this.sid = sid;
- }
-
- public String getSname() {
- return sname;
- }
-
- public void setSname(String sname) {
- this.sname = sname;
- }
- }
创建Servlet:SixServlet:
- package com.bjpowernode.controller;
-
- import com.bjpowernode.model.Student;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
-
- public class SixServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- Student stu=new Student(10,"mike");
- Student stu2=new Student(20,"allen");
-
- List list=new ArrayList();
- list.add(stu);
- list.add(stu2);
-
- request.setAttribute("key",list);
- request.getRequestDispatcher("/index_6.jsp").forward(request,response);
- }
- }
在web.xml:中指定访问方式:
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <servlet>
- <servlet-name>SixServletservlet-name>
- <servlet-class>com.bjpowernode.controller.SixServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>SixServletservlet-name>
- <url-pattern>/six.dourl-pattern>
- servlet-mapping>
-
- web-app>
index_6.jsp:
- <%@ page import="java.util.List" %>
- <%@ page import="com.bjpowernode.model.Student" %><%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 10:11
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <html>
- <head>
- <title>Titletitle>
- <base href="<%=basePath%>">
- head>
- <body>
- java命令获取数据<br>
- <table border="2">
- <tr>
- <td>学员编号td>
- <td>学员姓名td>
- tr>
- <%
- List
stuList=(List)request.getAttribute("key"); - for(Student stu:stuList){
- %>
- <tr>
- <td><%=stu.getSid()%>td>
- <td><%=stu.getSname()%>td>
- tr>
- <%
- }
- %>
- table>
- <hr>
- 通过JSTL标签来获取数据显示<br>
- <table border="2">
- <tr>
- <td>学员编号td>
- <td>学员姓名td>
- tr>
-
- <c:forEach items="${requestScope.key}" var="stu">
- <tr>
- <td>${stu.sid}td>
- <td>${stu.sname}td>
- tr>
- c:forEach>
- table>
- body>
- html>
(7)forEach遍历Map集合
控制器:SixServlet:
- package com.bjpowernode.controller;
-
- import com.bjpowernode.model.Student;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- public class SixServlet extends HttpServlet {
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- }
-
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- Student stu=new Student(10,"mike");
- Student stu2=new Student(20,"allen");
-
- //List集合
- List list=new ArrayList();
- list.add(stu);
- list.add(stu2);
-
- //Map集合
- Map map=new HashMap();
- map.put("一班",stu);
- map.put("二班",stu2);
- request.setAttribute("mapKey",map);
-
- request.setAttribute("key",list);
- request.getRequestDispatcher("/index_6.jsp").forward(request,response);
- }
- }
index_6.jsp:
- <%@ page import="java.util.List" %>
- <%@ page import="com.bjpowernode.model.Student" %><%--
- Created by IntelliJ IDEA.
- User: DELL
- Date: 2022/7/20
- Time: 10:11
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
- %>
- <%--需要加jstl的标签--%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
-
- <html>
- <head>
- <title>Titletitle>
- <base href="<%=basePath%>">
- head>
- <body>
- java命令获取数据<br>
- <table border="2">
- <tr>
- <td>学员编号td>
- <td>学员姓名td>
- tr>
- <%
- List
stuList=(List)request.getAttribute("key"); - for(Student stu:stuList){
- %>
- <tr>
- <td><%=stu.getSid()%>td>
- <td><%=stu.getSname()%>td>
- tr>
- <%
- }
- %>
- table>
- <hr>
- 通过JSTL标签来获取数据显示<br>
- <table border="2">
- <tr>
- <td>学员编号td>
- <td>学员姓名td>
- tr>
-
- <c:forEach items="${requestScope.key}" var="stu">
- <tr>
- <td>${stu.sid}td>
- <td>${stu.sname}td>
- tr>
- c:forEach>
- table>
-
- <hr>
- <h1>遍历map集合h1>
- <table border="2">
- <tr>
- <td>班级名称td>
- <td>学员编号td>
- <td>学员名称td>
- tr>
-
- <%--遍历Map集合每次从Map集合中得到的是一个【键值对】
- 再把【键值对】交给循环变量
- 循环变量.key获得【键值对】中关键字名字 班级名称
- 循环变量.value 获得【键值对】中的内容 stu对象
- --%>
- <c:forEach items="${mapKey}" var="key_Value">
- <tr>
- <td>${key_Value.key}td>
- <td>${key_Value.value.sid}td>
- <td>${key_Value.value.sname}td>
- tr>
- c:forEach>
- table>
- body>
- html>