• 【JSP】JSTL汇总——源码解析


    什么是JSTL

    JSTL全称为 Java Standard Tag Library(Java标准标签库)

    JSTL作为最基本的标签库,提供了一系列的JSP标签,实现了基本的功能:集合的遍历、数据的输出、字符串的处理等等

    使用JSTL可以让JSP代码更加的简洁,可读性非常好,重用性非常高,可以完成一些复杂的功能!

    使用JSTL的步骤

    1. 引入JSTL标签库对应的jar包

    在IDEA中怎么引入?
    WEB-INF下新建lib目录,然后将jar包拷贝到lib当中;
    什么时候需要将jar包放到WEB-INF下的lib目录下?
    Tomcat没有自带的jar

    1. 在JSP中引入要使用标签库(使用taglib指令引入标签库)

    JSTL中提供了很多标签库,重点掌握核心标签库(core标签库)

    在这里插入图片描述

    JSTL标签的原理

    <%taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    • 1

    以上uri后面的路径实际上指向了一个xxx.tld文件。
    tld文件实际上是一个xml配置文件。
    tld文件中描述了“标签”和“Java类”之间的关系,
    以上核心标签库对应的tld文件是:c.tld文件

    在这里插入图片描述

    分析标签源码

    <tag>
        <description>//标签对应的描述
            Catches any Throwable that occurs in its body and optionally
            exposes it.
        </description>
        <name>catch</name>//标签的名字
        <tag-class>org.apache.taglibs.standard.tag.common.core.CatchTag</tag-class>//标签对应的Java类
        
        <body-content>JSP</body-content>//标签体当中可以出现的内容,如果是JSP,就表示标签体中可以出现符合JSP所有语法的代码,例如EL表达式。
    
        <attribute>//属性
            <description>//属性的描述
    Name of the exported scoped variable for the
    exception thrown from a nested action. The type of the
    scoped variable is the type of the exception thrown.
            </description>
            
            <name>var</name>//属性名
            <required>false</required>
    //false表示该属性不是必须的,true表示该属性是必须的
            <rtexprvalue>false</rtexprvalue>
    //这个描述说明了该属性是否支持EL表达式,false表示不支持,true表示支持EL表达式
        </attribute>
    /*
    
    	JSP....
    
    */
    
      </tag>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    看核心标签库中的forEach标签

    下面是forEach标签的源码(有点小长):

    <tag>
        <description>
    	The basic iteration tag, accepting many different
            collection types and supporting subsetting and other
            functionality
        </description>
        <name>forEach</name>
        <tag-class>org.apache.taglibs.standard.tag.rt.core.ForEachTag</tag-class>
        <tei-class>org.apache.taglibs.standard.tei.ForEachTEI</tei-class>
        <body-content>JSP</body-content>//该标签允许JSP内容
        
        <attribute>
            <description>
    Collection of items to iterate over.
    (翻译:要遍历的集合)
            </description>
    	<name>items</name>
    	<required>false</required>
    	<rtexprvalue>true</rtexprvalue>
    	//该属性值支持EL表达式
    	
    	<type>java.lang.Object</type>
            <deferred-value>
    	    <type>java.lang.Object</type>
            </deferred-value>
        </attribute>
    
    
    
        <attribute>
            <description>
    If items specified:
    Iteration begins at the item located at the
    specified index. First item of the collection has
    index 0.
    If items not specified:
    Iteration begins with index set at the value
    specified.
            </description>
    	<name>begin</name>
    	<required>false</required>
    	<rtexprvalue>true</rtexprvalue>
    	<type>int</type>
        </attribute>
    
    
        <attribute>
            <description>
    If items specified:
    Iteration ends at the item located at the
    specified index (inclusive).
    If items not specified:
    Iteration ends when index reaches the value
    specified.
            </description>
    	<name>end</name>
    	<required>false</required>
    	<rtexprvalue>true</rtexprvalue>
    	<type>int</type>
        </attribute>
    
    
        <attribute>
            <description>
    Iteration will only process every step items of
    the collection, starting with the first one.
            </description>
    	<name>step</name>
    	<required>false</required>
    	<rtexprvalue>true</rtexprvalue>
    	<type>int</type>
        </attribute>
    
    
        <attribute>
            <description>
    Name of the exported scoped variable for the
    current item of the iteration. This scoped
    variable has nested visibility. Its type depends
    on the object of the underlying collection.
            </description>
    (翻译:迭代的当前项的导出作用域变量的名称。这个作用域变量具有嵌套的可见性。它的类型取决于基础集合的对象。)
    
    	<name>var</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
    	//不可以用EL表达式
        </attribute>
    
    
        <attribute>
            <description>
    Name of the exported scoped variable for the
    status of the iteration. Object exported is of type
    jakarta.servlet.jsp.jstl.core.LoopTagStatus. This scoped variable has nested
    visibility.
            </description>
    	<name>varStatus</name>
    	<required>false</required>
    	<rtexprvalue>false</rtexprvalue>
        </attribute>
      </tag>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 在此标签属性items中,该属性的属性值是支持EL表达式的,那么就可以像下面这样使用:

    在这里插入图片描述

    • 在此标签属性var中,该属性代表的是集合中的每一个元素,var后面的名字是自取的是随意的。

    在这里插入图片描述
    测试结果:
    在这里插入图片描述

    主标签库常用标签

    forEach标签

    1. 上面解释了的forEach中的var和items属性标签

    begin、end、step属性

    <%@page contentType="text/html;charset=UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <c:forEach var="i" begin="1" end="10" step="1">
      ${i}<br>
    </c:forEach>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    测试结果:
    在这里插入图片描述
    由于可以使用EL表达式去访问var,这里我们可以猜测var被存在某个域中了;

    stuStatus属性

    varStatus=“这个属性表示var的状态对象,这里是一个Java对象,这个Java对象代表了var的状态”
    varStatus=“这里面名字是随意的”
    varStatus这个状态对象有count属性,可以直接使用。

    <%@ page import="javawen.jsp.bean.User" %>
    <%@ page import="java.util.List" %>
    <%@ page import="java.util.ArrayList" %>
    <%@page contentType="text/html;charset=UTF-8" %>
    
    <%--引入标签库--%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <%
        List<User> list = new ArrayList<>();
        User user1 = new User("zx","13",34);
        User user2 = new User("zs","343",94);
        User user3 = new User("xm1","12413",24);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        request.setAttribute("user",list);
    %>
    
    <%--items通过源码我们知道是支持EL表达式的--%>
    
    <c:forEach items="${user}" var="u" varStatus="xx">
        编号:${xx.count},name:${u.username},password:${u.password},age:${u.age}
        <br>
    </c:forEach>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    测试结果:

    在这里插入图片描述

    if标签

    1. if标签(test属性(必须),var属性(非必须),scope属性(非必须))

    test属性

    看下面源码可以知道,if标签中test属性必须存在并且是支持EL表达式的;
    在这里插入图片描述

    <%@page contentType="text/html;charset=UTF-8"%>
    
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <c:if test="${empty paramValues.username}">
        <h1>用户名为空</h1>
    </c:if>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    测试效果:
    在这里插入图片描述

    var和scope

    var=“这里填的是一个变量”,这个变量名是布尔类型的,其值和test相对应。
    下面看看var属性描述的翻译:
    在这里插入图片描述
    scope=“这里填的是四大作用域中的其中一个”,可以填(pageContext、request、session、application)。
    表示var作用的范围!

    下面是其属性描述翻译:
    在这里插入图片描述
    测试:

    <%@page contentType="text/html;charset=UTF-8"%>
    
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    <c:if test="${empty paramValues.username}" var="flag" scope="application">
        <h1>用户名为空</h1>
    </c:if>
    
    ${flag}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    choose和when标签

    <%@page contentType="text/html;charset=UTF-8" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%--
      下面相当于
      if(){
      }else if(){
      }else if(){
      }else{
      }
    
    --%>
    <c:choose>
      <c:when test="${param.age<18}">
        青少年
      </c:when>
      <c:when test="${param.age<35}">
        青年
      </c:when>
      <c:when test="${param.age<55}">
        中年
      </c:when>
      <c:otherwise>
        老年
      </c:otherwise>
    
    </c:choose>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    测试结果:

    在这里插入图片描述

  • 相关阅读:
    数据驱动成功:小程序积分商城的数据分析
    Divide by 2 or 3
    Python之Python的版本选择和IDE工具选择问题
    JS 中防抖函数形成闭包的相关处理及思考
    研发挑战的本原
    大营销抽奖系统,DDD开发要如何建模?
    SpringBoot集成EasyExcel快速人们
    【JVM基础14】——垃圾回收-强引用、软引用、弱引用、虚引用的区别
    gitlab无法push(pre-receive hook declined)
    ZYNQ搭建HP总线从DDR进行PL与PS交互
  • 原文地址:https://blog.csdn.net/qq_63691275/article/details/128129964