12、JSTL

JSTL

JSP标准标签库(JSTL)是一个JSP标签集合,它封装了JSP应用的通用核心功能。

1、引入jstl.jar和standard.jar包在lib目录;

2、在JSP页面中引入标签库;

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

3、JSTL的分类:

  • core:核心类库 ★
  • fmt:格式化|国际化
  • xml:过时了
  • sql:过时了
  • 函数库:很少使用

core

  • c:if
  • c:forEach

if标签

<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--引入JSTL标签的核心库--%>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        request.setAttribute("key", null);
        
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(100);
        request.setAttribute("value", arrayList);
    %>
    <c:if test="${empty requestScope.key}">
        ${requestScope.key}
        当用EL表达式取域中的属性时,当属性值为null时,在页面不显示。
    </c:if>
    <br>
    <c:if test="${not empty requestScope.value}">
        ${requestScope.value[0]}
    </c:if>
</body>
</html>

在这里插入图片描述

forEach标签

<%@ page import="java.util.ArrayList" %>
<%@ page import="org.westos.pojo.User" %>
<%@ page import="java.util.HashMap" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        int[] arr = new int[]{10, 20, 30, 40, 50};
        request.setAttribute("arr", arr);
    %>

    <%--
        新式for循环,items:容器,var:当前遍历的元素
        v.count:元素编号,从1开始
        v.index:元素索引
        v.current:当前元素的值
    --%>
    <c:forEach items="${arr}" var="ele" varStatus="v">
        ${ele}-${v.count}-${v.index}-${v.current}
    </c:forEach>
    <hr>

    <%
        ArrayList<User> users = new ArrayList<>();
        users.add(new User("张三", 23));
        users.add(new User("李四", 24));
        request.setAttribute("users", users);
    %>
    <c:forEach items="${users}" var="ele" varStatus="v">
        ${v.index}-${ele.username}-${ele.age}
    </c:forEach>
    <hr>
    <%
        HashMap<String, User> hashMap = new HashMap<>();
        hashMap.put("zs", new User("张三", 23));
        hashMap.put("ls", new User("李四", 24));
        request.setAttribute("map", hashMap);
    %>
    <%--双列集合的var是Map集合的键值对Entry对象--%>
    <c:forEach items="${map}" var="ele">
        ${ele.key}-${ele.value.username}-${ele.value.age}
    </c:forEach>
</body>
</html>

在这里插入图片描述

set标签、out标签

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:set var="username" value="john" scope="request"/>
    <%--保存数据到域中,默认保存到pageContext域--%>

    <c:out value="${requestScope.username}" default="<h3>标题三</h3>"/>
</body>
</html>

在这里插入图片描述

choose标签、when标签、otherwise标签

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <c:set var="score" scope="request" value="57"/>

    <c:choose>
        <c:when test="${requestScope.score >= 90 && requestScope.score <= 100}">
            优秀
        </c:when>
        <c:when test="${requestScope.score >= 80 && requestScope.score < 90}">
            良好
        </c:when>
        <c:when test="${requestScope.score >= 70 && requestScope.score < 80}">
            一般
        </c:when>
        <c:when test="${requestScope.score >= 60 && requestScope.score < 70}">
            及格
        </c:when>
        <c:otherwise>
            不及格
        </c:otherwise>
    </c:choose>
</body>
</html>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值