jsp的JSTL详解

一、什么是JSTL

1.1 JSTL简介

JSTL全称:JSP Standard Tag Library(JSP标准标签库),是一个一段完善的开源的JSP标签库。
作用:替代jsp的代码脚本。
优点:让jsp页面变得更简洁。

1.2 JSTL的五个标签库

在这里插入图片描述
要想在jsp中引用JSTL,我们需要在jsp文件中使用taglib指令引入标签库,具体引入如下:
在这里插入图片描述

1.3 JSTL的使用步骤

1、导入JSTL标签库jar包(最后边有下载教程)
2、引入标签库,指令如下(如果是其他的标签就用其他的指令,引入写在jsp文件打头位置):

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

二、JSTL库的使用

2.1 core核心库使用

c:set标签

作用:向域中保存数据
用法:

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

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%--
      c:set各属性简单介绍:
        scope属性指定哪个域(默认是page)
        var是键
        value是值
    --%>
    未设置的request.key值:${requestScope.key}
    <c:set scope="request" var="key" value="RequestValue"/>
    设置后的request.key值:${requestScope.key}
</body>
</html>

运行结果:
在这里插入图片描述

c:if标签

作用:做if判断
用法:

    <%--
        c:if属性介绍:
            test:判断的条件(使用EL表达式输出)
            如果满足条件,那么执行条件体内的内容,否则不执行
    --%>
    <c:if test="${1+1 == 2}">
        <h1>1 + 1 = 2</h1>
    </c:if>

在这里插入图片描述
注意:没有if-else这种语句,如果一定要写的话,那就只能用多个<c:if>标签来表示。

c:choose、c:when、c:otherwise标签

作用:多路判断,类似于switch吧,然后case什么什么,最后default。
用法:

    <c:set scope="request" var="height" value="186"/>
    <%--
        choose标签开始选择判断
        when表示每一种判断情况
        otherwise表示除了上面的情况
    --%>
    <c:choose>
        <c:when test="${requestScope.height >= 180}">
            <h2>大高个</h2>
        </c:when>
        <c:when test="${requestScope.height >= 170}">
            <h2>中高个</h2>
        </c:when>
        <c:when test="${requestScope.height >= 160}">
            <h2>小高个</h2>
        </c:when>
        <c:otherwise>
            <h2>吹不下去了</h2>
        </c:otherwise>
    </c:choose>

显示:
在这里插入图片描述
注意:when标签的父标签只能是choose标签,不能直接在when中嵌套when,如果一定要写多级判断的话,请在when中使用choose标签再写when标签 或 在when中嵌套if标签。

c:forEach标签

作用:遍历数组、集合等
用法一、遍历某个数值范围:

    <%--
        forEach属性介绍:
            begin:开始的索引
            end:结束的索引
            var:当前遍历到的数据的变量
    --%>
    <c:forEach begin="1" end="10" var="num">
        ${num}<br>
    </c:forEach>

显示:
在这里插入图片描述
用法二:动态布局我们的页面:

    <table border="1">
        <c:forEach begin="1" end="10" var="col">
            <tr>
                <td>第${col}行</td>
            </tr>
        </c:forEach>
    </table>

在这里插入图片描述
用法三:遍历数组:

    <c:set scope="request" var="arr" value="asd,23a,56s"/>
    <%--
        遍历数组
        	items属性表示遍历的数据源
        	var是目前遍历的数据的变量名
    --%>
    <c:forEach items="${requestScope.arr}" var="item">
        ${item}<br>
    </c:forEach>

显示:
在这里插入图片描述
用法四:遍历List集合:
这里来点厉害点的,假如有个学生类如下:

package com.hstc.edu;

public class Student {
    private Integer id;
    private String username;
    private String password;

    public Student() {
    }

    public Student(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

那么,我们输出这个学生类的集合的代码如下(如果我们的数据是从数据库读出来的,那么用来显示就会方便很多了):

    <%
        Student student = new Student(1,"username","password");
        List<Student> list = new ArrayList<Student>();
        list.add(student);
        list.add(student);
        list.add(student);
        list.add(student);
        request.setAttribute("list",list);
    %>
    <table border="1">
        <tr>
            <th>学号</th>
            <th>账号</th>
            <th>密码</th>
        </tr>
        <c:forEach items="${requestScope.list}" var="item">
            <tr>
                <td>${item.id}</td>
                <td>${item.username}</td>
                <td>${item.password}</td>
            </tr>
        </c:forEach>
    </table>

显示:
在这里插入图片描述

用法五:遍历Map集合:

    <%--遍历Map列表--%>
    <%
        Map<String, Object> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        request.setAttribute("map",map);
    %>
    <c:forEach items="${requestScope.map}" var="item">
        item = ${item}<br>
        key = ${item.key}, value = ${item.value}<br>
    </c:forEach>

显示:(直接输出item,则键值对会以 key=value的形式输出,使用.key可以输出键,.value可以输出值)
在这里插入图片描述

补充forEach标签的知识点

知识点1:begin和end属性的妙用
forEach标签的begin属性和end属性也可以用来设置输出的范围,比如:
以下代码会输出key2——key6的信息(begin和end的索引值从0开始计数,索引范围是begin——end,需要注意的是,end不需要减1)

    <%
        Map<String, Object> map = new HashMap<>();
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3","value3");
        map.put("key4","value4");
        map.put("key5","value5");
        map.put("key6","value6");
        map.put("key7","value7");
        map.put("key8","value8");
        request.setAttribute("map",map);
    %>
    <c:forEach begin="1" end="5" items="${requestScope.map}" var="item">
        item = ${item}<br>
        key = ${item.key}, value = ${item.value}<br>
    </c:forEach>

显示:(解释一下为什么会乱序,因为我们用了HashMap这个类
在这里插入图片描述
知识点2:forEach标签的step属性用来设置遍历的步长值
比如当step=2的时候,就会输出索引为0、2、4、6、8 。。。的数据。

知识点3:forEach标签的varStatus属性
用来查看当前遍历对象的具体信息、索引、遍历个数等等信息,具体查看的接口信息:
在这里插入图片描述
当我们设置了该属性varStatus = “statu”,那么:
就可以使用
${statu.current} 查看他遍历到的数据
${statu.index}查看他的索引
${statu.first}查看他是否是第一个索引
${statu.begin}查看他的begin的属性值

三、JSTL标签库jar包下载

官方下载路径:https://tomcat.apache.org/download-taglibs.cgi
进入后往下拉,下载红色框起来的这两个即可。
在这里插入图片描述

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值