JSTL标签

使用变迁库,你必须在每个JSP文件中的头部包含<taglib>标签。

核心标签引入语法:<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!--导入标准库-->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        table{
            border-collapse: collapse;
            border: 1px #888888 solid;
        }
        td,th{
            padding:4px;
            border: 1px #888888 solid;
        }
    </style>
</head>
<body>
<%
    String strs[] = {"张三","李四","王五","赵刘"};
    request.setAttribute("strs",strs);

    String names = "张1三,李1四,王1五,赵1刘";
    request.setAttribute("names",names);
%>
</body>
<table>
    <tr>
    <th>name</th>
    <th>begin</th>
    <th>count</th>
    <th>current</th>
    <th>end</th>
    <th>first</th>
    <th>last</th>
    <th>index</th>
    <th>step</th>
    </tr>
    <!--遍历数组-->
    <c:forEach items="${requestScope.strs}" var="name" varStatus="status">
        <tr>
        <td>${name}</td>
        <td>${status.begin}</td>
        <td>${status.count}</td>
        <td>${status.current}</td>
        <td>${status.end}</td>
        <td>${status.first}</td>
        <td>${status.last}</td>
        <td>${status.index}</td>
        <td>${status.step}</td>
        </tr>
    </c:forEach>

</table>


<!--if else-->
<c:choose>
    <c:when test="${param.age<18}">
        <h4>未成年</h4>
    </c:when>
    <c:otherwise>
        <h4>成年</h4>
    </c:otherwise>
</c:choose>



<table>
    <tr>
        <th>step步长</th>
        <th>begin开始值</th>
        <th>end结束值</th>
        <th>index下标</th>
        <th>count计数</th>
        <th>current遍历当前的值</th>
    </tr>
    <c:forEach begin="2" end="10" step="3" varStatus="status">
       <tr>
           <th>${status.step}</th>
           <th>${status.begin}</th>
           <th>${status.end}</th>
           <th>${status.index}</th>
           <th>${status.count}</th>
           <th>${status.current}</th>

       </tr>
    </c:forEach>
</table>

<c:forTokens items="${requestScope.names}" delims="," var="name">
    <p>${name}</p>
</c:forTokens>

<!--将var-value 的键值对保存在scope指定的作用域中-->
<c:set var="path" value="${pageContext.request.contextPath}" scope="request"/>
<%--向页面中输出value 中的值 默认 escapeXml="true" 不解析html标签--%>
<c:out value="<h1>这是一个h1标签</h1>" escapeXml="false"/>
<%--删除 request作用域中名为path的 的键值对--%> 
<c:remove var="path" scope="request"/>
<%--捕获异常 将异常的信息放在e中 如果没有发生异常 e == null--%>
<c:catch var="e">
    <%
        int i=1/0;
    %>
</c:catch>

<%--导入其他的页面到当前页--%>
<c:import url="index.jsp"/>

<%--重定向到url指定的页面--%>
<c:redirect url="index.jsp"/>
<%--向重定向的页面传递参数--%>
<c:url var="myURL" value="index.jsp">
    <c:param name="age" value="12"/>
    <c:param name="name" value="shand"/>
</c:url>


<a href="<c:out value="${myURL}"/>">给页面传递参数age=12 name=shand</a>

</html>
原文地址:https://www.cnblogs.com/yuing/p/8793336.html