1 概述

JSP标准标签库(Jsp Standarded Tag Library) ,使用标签取代JSP页面上的Java代码。如下代码就是JSTL标签

  1. <c:if test="${flag == 1}">
  2. </c:if>
  3. <c:if test="${flag == 2}">
  4. </c:if>

上面代码看起来是不是比 JSP 中嵌套 Java 代码看起来舒服好了。而且前端工程师对标签是特别敏感的,他们看到这段代码是能看懂的。
JSTL 提供了很多标签,如下图image-20210818153646575.png
我们只对两个最常用的标签进行讲解, 标签和 标签。
JSTL 使用也是比较简单的,分为如下步骤:

  • 导入坐标

    1. <dependency>
    2. <groupId>jstl</groupId>
    3. <artifactId>jstl</artifactId>
    4. <version>1.2</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>taglibs</groupId>
    8. <artifactId>standard</artifactId>
    9. <version>1.1.2</version>
    10. </dependency>
  • 在JSP页面上引入JSTL标签库

<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>

  • 使用标签

    2 if 标签

    :相当于 if 判断

  • 属性:test,用于定义条件表达式

    1. <c:if test="${flag == 1}">
    2. </c:if>
    3. <c:if test="${flag == 2}">
    4. </c:if>

    代码演示:

  • 定义一个 servlet ,在该 servlet 中向 request 域对象中添加 键是 status ,值为 1 的数据

    1. @WebServlet("/demo2")
    2. public class ServletDemo2 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    5. //1. 存储数据到request域中
    6. request.setAttribute("status",1);
    7. //2. 转发到 jstl-if.jsp
    8. 数据request.getRequestDispatcher("/jstl-if.jsp").forward(request,response);
    9. }
    10. @Override
    11. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    12. this.doGet(request, response);
    13. }
    14. }

    定义 jstl-if.jsp 页面,在该页面使用 标签

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    3. <html>
    4. <head>
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <%--
    9. c:if:来完成逻辑判断,替换java if else
    10. --%>
    11. <c:if test="${status ==1}">
    12. 启用
    13. </c:if>
    14. <c:if test="${status ==0}">
    15. 禁用
    16. </c:if>
    17. </body>
    18. </html>

    ==注意:== 在该页面已经要引入 JSTL核心标签库
    <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core“ %>

    3 forEach 标签

    :相当于 for 循环。java中有增强for循环和普通for循环,JSTL 中的 也有两种用法

    3.1 用法一

    类似于 Java 中的增强for循环。涉及到的 中的属性如下

  • items:被遍历的容器

  • var:遍历产生的临时变量
  • varStatus:遍历状态对象

如下代码,是从域对象中获取名为 brands 数据,该数据是一个集合;遍历遍历,并给该集合中的每一个元素起名为 brand,是 Brand对象。在循环里面使用 EL表达式获取每一个Brand对象的属性值

  1. <c:forEach items="${brands}" var="brand">
  2. <tr align="center">
  3. <td>${brand.id}</td>
  4. <td>${brand.brandName}</td>
  5. <td>${brand.companyName}</td>
  6. <td>${brand.description}</td>
  7. </tr>
  8. </c:forEach>

代码演示:

  • servlet 还是使用之前的名为 ServletDemo1 。
  • 定义名为 jstl-foreach.jsp 页面,内容如下 ```java <%@ page contentType=”text/html;charset=UTF-8” language=”java” %> <%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core“ %>

<!DOCTYPE html>



  1. <c:forEach items="${brands}" var="brand" varStatus="status">
  2. <tr align="center">
  3. <%--<td>${brand.id}</td>--%>
  4. <td>${status.count}</td>
  5. <td>${brand.brandName}</td>
  6. <td>${brand.companyName}</td>
  7. <td>${brand.ordered}</td>
  8. <td>${brand.description}</td>
  9. <c:if test="${brand.status == 1}">
  10. <td>启用</td>
  11. </c:if>
  12. <c:if test="${brand.status != 1}">
  13. <td>禁用</td>
  14. </c:if>
  15. <td><a href="#">修改</a> <a href="#">删除</a></td>
  16. </tr>
  17. </c:forEach>

序号 品牌名称 企业名称 排序 品牌介绍 状态 操作

  1. <a name="RiYxn"></a>
  2. #### 3.2 用法二
  3. 类似于 Java 中的普通for循环。涉及到的 <c:forEach> 中的属性如下
  4. - begin:开始数
  5. - end:结束数
  6. - step:步长
  7. 实例代码:<br />从0循环到10,变量名是 i ,每次自增1
  8. ```java
  9. <c:forEach begin="0" end="10" step="1" var="i">
  10. ${i}
  11. </c:forEach>