• JSTL 标签库


    JSTL 标签库

    JSTL 标签库 全称是指 JSP Standard Tag Library JSP 标准标签库。是一个不断完善的开放源代码的 JSP 标签库。
    EL 表达式主要是为了替换 jsp 中的表达式脚本,而标签库则是为了替换代码脚本。这样使得整个 jsp 页面变得更佳简洁。

    1. 标签

    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

    <%
    request.setAttribute("height", 180);
    %>
    <c:choose>
    <%-- 这是 html 注释 --%>
    <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>
    
    • 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

    3.
    作用:遍历输出使用。
    begin 属性设置开始的索引
    end 属性设置结束的索引
    var 属性表示循环的变量(也是当前正在遍历到的数据)

    <table border="1">
    <c:forEach begin="1" end="10" var="i">
    <tr>
    <td>第${i}</td>
    </tr>
    </c:forEach>
    </table>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    遍历 Object 数组
    items 表示遍历的数据源(遍历的集合)
    var 表示当前遍历到的数据

    <%
    request.setAttribute("arr", new String[]{"18610541354","18688886666","18699998888"});
    %>
    <c:forEach items="${ requestScope.arr }" var="item">
    ${ item } <br>
    </c:forEach>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    遍历 Map 集合

    <%
    Map<String,Object> map = new HashMap<String, Object>();
    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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    【Python】sys.argv[ ]简单易懂
    软件测试 -- 入门 4 软件测试原则
    php 对接IronSource海外广告平台收益接口Reporting API
    【无标题】后来,我认为王阳明比尼采,叔本华都高明
    给新入坑的小伙伴们的郑氏Java上路指南
    2022谷粒商城学习笔记(六)JSR303数据校验和集中异常处理
    Kotlin面向对象基础使用方法(继承、接口、Lambda、空指针检查机制等)
    【JS】牛客专项练习01
    linux之ftp服务-1
    Unity3D PRO 3D游戏制作系列教程第四课:认识菜单二
  • 原文地址:https://blog.csdn.net/qq_52896055/article/details/127645338