JSTL标签库

8 篇文章 2 订阅

目录

1.简介

2.JSTL标签库的使用步骤

3.core核心库使用

3.1、set

3.2、if

3.2、choose、when、otherwise

3.4、forEach


1.简介

JSTL 标签库 全称是指 JSP Standard Tag Library JSP 标准标签库。是一个不断完善的开放源代码的 JSP 签库。

EL 表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个 jsp 页面变得更佳简洁。

JSTL 由五个不同功能的标签库组成

jsp 标签库中使用 taglib 指令引入标签库

    CORE 标签库
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    XML 标签库
    <%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
    FMT 标签库
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    SQL 标签库
    <%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
    FUNCTIONS 标签库
    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

2.JSTL标签库的使用步骤

1、先导入 jstl 标签库的 jar 包。

taglibs-standard-impl-1.2.1.jar

taglibs-standard-spec-1.2.1.jar

2、第二步,使用 taglib 指令引入标签库。

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

 

3.core核心库使用

3.1、set

作用:set 标签可以往域中保存数据

代码示例:

    <%--
    i.<c:set />
        作用:set标签可以往域中保存数据

        域对象.setAttribute(key,value);
        scope 属性设置保存到哪个域
            page表示PageContext域(默认值)
            request表示Request域
            session表示Session域
            application表示ServletContext域
        var属性设置key是多少
        value属性设置值
    --%>

    保存之前:${ requestScope.abc }<br/>
    <c:set scope="request" var="abc" value="abcValue"/>
    保存之后:${ requestScope.abc }<br/>

截图展示:

 

3.2、if

if 标签用来做 if 判断。

代码示例:

    <%--
       ii.<c:if />
         if标签用来做if判断。
         test属性表示判断的条件(使用EL表达式输出)
    --%>
    <c:if test="${ 12 == 12 }">
        <h1>12等于12</h1>
    </c:if>
    <c:if test="${ 12 != 12 }">
        <h1>12不等于12</h1>
    </c:if>

截图示例:

 

3.2、choose、when、otherwise

作用:多路判断。跟 switch ... case .... default 非常接近

注意:

1、标签里不能使用html注释,要使用jsp注释
        2、when标签的父标签一定要是choose标签

代码示例:

    <%--
    iii.<c:choose> <c:when> <c:otherwise>标签
    作用:多路判断。跟switch ... case .... default非常接近

    choose标签开始选择判断
    when标签表示每一种判断情况
        test属性表示当前这种判断情况的值
    otherwise标签表示剩下的情况

    <c:choose> <c:when> <c:otherwise>标签使用时需要注意的点:
        1、标签里不能使用html注释,要使用jsp注释
        2、when标签的父标签一定要是choose标签
    --%>
    <%
        request.setAttribute("height",157);
    %>
    <c:choose>
        <c:when test="${ requestScope.height > 190 }">
            <h2>小巨人</h2>
        </c:when>
        <c:when test="${ requestScope.height > 180 }">
            <h2>很高</h2>
        </c:when>
        <c:when test="${ requestScope.height > 170 }">
            <h2>还可以</h2>
        </c:when>
        <c:otherwise>
            <c:choose>
                <c:when test="${requestScope.height > 160}">
                    <h3>大于160</h3>
                </c:when>
                <c:when test="${requestScope.height > 150}">
                    <h3>大于150</h3>
                </c:when>
                <c:when test="${requestScope.height > 140}">
                    <h3>大于140</h3>
                </c:when>
                <c:otherwise>
                    其他小于140
                </c:otherwise>
            </c:choose>
        </c:otherwise>
    </c:choose>

截图示例:

 

3.4、forEach

作用:遍历输出使用。

代码示例:

1、遍历 1 到 10,输出

    <%--1.遍历1到10,输出
        begin属性设置开始的索引
        end 属性设置结束的索引
        var 属性表示循环的变量(也是当前正在遍历到的数据)
        for (int i = 1; i < 10; i++)
	--%>
    <table border="1">
        <c:forEach begin="1" end="10" var="i">
            <tr>
                <td>第${ i }行</td>
            </tr>
        </c:forEach>
    </table>

2、遍历 Object 数组

    <%-- 2.遍历Object数组
            for (Object item: arr)
            items 表示遍历的数据源(遍历的集合)
            var 表示当前遍历到的数据
    --%>
    <%
        request.setAttribute("arr",new String[]{"13611112222","18722223333","18699998888"});
    %>
    <c:forEach items="${ requestScope.arr }" var="item">
        ${ item }<br/>
    </c:forEach>

3、遍历 List 集合----List 中存放 Student 类,有属性:编号,用户名,密码,年龄,电话信息

Student 类

package pojo;

/**
 * @author LIXICHEN
 * @create 2020-04-23 1:43
 */
public class Student {

//    编号,用户名,密码,年龄,电话信息
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private String phone;


    public Student() {
    }

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


    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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

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

测试代码:

<%--4.遍历List集合---list中存放 Student类,有属性:编号,用户名,密码,年龄,电话信息--%>
    <%
        List<Student> studentList = new ArrayList<Student>();
        for (int i = 0; i < 10; i++) {
            int t = i + 1;
            studentList.add(new Student(t, "username" + t,"pass"+t,18+t,"phone"+t));
        }
        request.setAttribute("stus",studentList);
    %>
    <table>
        <tr>
            <th>编号</th>
            <th>用户名</th>
            <th>密码</th>
            <th>年龄</th>
            <th>电话</th>
            <th>操作</th>
        </tr>
            <%--
            items 表示遍历的集合
            var 表示遍历到的数据
            begin表示遍历的开始索引值
            end 表示结束的索引值
            step 属性表示遍历的步长值
            varStatus 属性表示当前遍历到的数据的状态
            for(int i = 1; i < 10; i+=2)
        --%>
            <c:forEach begin="2" end="7" step="2" varStatus="status" items="${requestScope.stus}" var="stu">
                <tr align="center">
                    <td>${stu.id}</td>
                    <td>${stu.username}</td>
                    <td>${stu.password}</td>
                    <td>${stu.age}</td>
                    <td>${stu.phone}</td>
                    <td>${status.step}</td>
                </tr>
            </c:forEach>

    </table>

 

4、遍历 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="entry">

        <h1>${ entry.key } = ${ entry.value }</h1>

    </c:forEach>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值