Overview of JSTL Tag Library-Use of <c:if> and <c:forEach>

JSTL overview

  • JSTL (JSP Standard Tag Library), the JSP standard tag library, can be embedded in the JSP page in the form of tags to complete business logic and other functions.
  • Meaning: The purpose of jstl is to replace the script code in the jsp page, just like el.

JSTL standard tag library

The main ones used are the if and forEach tags in the Core sub-library

Insert picture description here

Environment: need to import jar package and import jstl tag library

  Import the jar package
Insert picture description here
  into the jspl tag library

<%--在jsp页面引入jstl标签库--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"   prefix="c"%>

if tag <c:if> (mainly master the test attribute)

  The test attribute determines the boolean value returned by the content expression in the tag body. If it is true, execute it, and false does not execute the
Insert picture description here
  case:

<c:if test="${10 > 8 }" var="bl" scope="session">
	<h1 style="color: red">10大于8</h1>
</c:if>

forEach tag <c:forEach> (mainly master the var items attribute)

Insert picture description here
  Case:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.ArrayList"  %>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

<%
	//java代码,赋初值
    ArrayList<String> strList = new ArrayList<String>();
    strList.add("我是list111");
    strList.add("我是list222");
    strList.add("我是list333");
    strList.add("我是list444");
    strList.add("我是list555");
    strList.add("我是list666");
	//将strList放入请求作用域中
    request.setAttribute("strList", strList);
%>

%--
   items="${strList}" 从域中根据strList这个键获取集合对象
   var="str"       每次循环时,jstl会自动将集合中的元素赋给var
                   每次循环时,jstl会自动将var的值存入pageContext域
   varStatus="vs"  这个参数会记录当前循环的一些状态信息
            vs.count  可以获取当前循环的次数
--%>

<c:forEach items="${strList}" var="str" varStatus="vs">
    <table border="1px" cellspacing="0" cellpadding="0" align="centers">
        <thead>
            <tr>
                <th>顺序</th>
                <th>strList值</th>
            </tr>
        </thead>
        <tbody>
        <tr>
            <td>${
    
    vs.count}</td>
            <td>${
    
    str}</td>
        </tr>
        </tbody>
    </table>
</c:forEach>

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40542534/article/details/108598181