【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" %>

以上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>
/*
<c:catch var="">
	JSP....
</c:catch>
*/

  </tag>

看核心标签库中的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>
  • 在此标签属性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>

测试结果:
在这里插入图片描述
由于可以使用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>


测试结果:

在这里插入图片描述

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>

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

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}

在这里插入图片描述

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>

测试结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假正经的小柴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值