[Study notes] JSTL tag library

1. Concept

The full name of JSTL tag library refers to JSP Standard Tag Library. It is an open source JSP tag library that is constantly improving.
The EL expression is mainly to replace the expression script in the jsp, and the tag library is to replace the code script. This makes the entire jsp page better and more concise.
Insert picture description here
Use the taglib instruction in the jsp tag library to introduce the tag library
format
<%@ taglib prefix=" prefix" uri= "URL" %>

Second, use steps

  1. Import the jar package of the jstl tag library
    Insert picture description here
    Note: The lib file is placed under WEB-INF
    Insert picture description here

Note: lib files are placed under WEB-INF
Insert picture description here

  1. Use the taglib instruction to introduce the tag library
    <%@ taglib prefix="c" url="http://java.sun.com/jsp/jstl/core" %>

Third, the use of core libraries

<c:set />

Function: The set tag can save data to the domain

    <body>
        ${sessionScope.key01}<%--保存之前--%>
        <c:set scope="session" var="key01" value="value"/>
        ${sessionScope.key01}<%--保存之前--%>
 	 </body>

<c:if />

The if tag is used to make if judgments.

    <body>
    <c:if test="${12==12}">
        <h1>12 = 12</h1>
    </c:if>
  </body>

<c:choose> <c:when> <c:otherwise>

Role: Multi-way judgment. Very close to switch… case… default.

    <body>
    <c:set scope="page" var="key" value="180"/>
    
    <c:choose>
        <c:when test="${pageScope.key<180}">
            <h1>小于180</h1>
        </c:when>
        <c:when test="${pageScope.key==180}">
            <h1>等于180</h1>
        </c:when>
        <c:when test="${pageScope.key>180}">
            <h1>大于180</h1>
        </c:when>
        <c:otherwise>
            <h1>啥也不是</h1>
        </c:otherwise>
    </c:choose>

  </body>

<c:forEach />

Role: traverse output use.

Traverse 1~10

   <%--
        begin 属性设置开始的索引
        end 属性设置结束的索引
        var 属性表示循环的变量(也是当前正在遍历到的数据)
        step 属性表示遍历的步长值
    --%>
        <c:forEach begin="1" end="10" var="i">
            ${
    
    i}
        </c:forEach>

Traverse the Object array

    <body>
    <%--
        items 表示遍历的数据源(遍历的集合)
        var 表示当前遍历到的数据
    --%>
    <%
        pageContext.setAttribute("arr",new String[]{
    
    "123","456"});
    %>
    <c:forEach items="${pageScope.arr}" var="item">
        ${
    
    item}
    </c:forEach>
    </body>

Traverse the map

    <body>
    <%--
        items 表示遍历的数据源(遍历的集合)
        var 表示当前遍历到的数据
    --%>
    <%
        Map map = new HashMap();
        map.put("k1","v1");
        map.put("k2","v2");
        pageContext.setAttribute("map",map);
    %>
    <c:forEach items="${pageScope.map}" var="entry">
        ${
    
    entry.key} = ${
    
    entry.value}
    </c:forEach>

    </body>

Guess you like

Origin blog.csdn.net/DREAM_yao/article/details/114154297