JSTL标准标签库

现有问题

  • EL主要是用于作用域获取数据,虽然可以做运算判断,但是得到的都是一个结果,做展示.

  • EL不存在流程控制。比如判断.

  • EL对集合只能做单点访问,不能实现遍历操作,比如循环.

什么是JSTL

  • JSTL:全称Java Server Pages Standard Tag Library

  • JSP标准标签库(JSTL)是JSP标签集合。

JSTL的作用

  • 可对EL获取到的数据进行逻辑操作。

  • 与EL合作完成数据的展示。

JSTL使用

<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>taglibs</groupId>
    <artifactId>standard</artifactId>
    <version>1.1.2</version>
</dependency>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

判断、遍历、重写URL

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

    <%--判断--%>
    <c:if test="${2>1}">
        <h2>2>1</h2>
    </c:if>
    <c:if test="${2<=1}">
        <h2>2<=1</h2>
    </c:if>

    <%--多条件判断,像switch--%>
    <%
        request.setAttribute("grade",50);
    %>
    <c:choose>
        <c:when test="${grade >= 80}">
            <h2>优秀</h2>
        </c:when>
        <c:when test="${grade >= 60 && grade <80}">
            <h2>良好</h2>
        </c:when>
        <c:otherwise>
            <h2>不及格</h2>
        </c:otherwise>

    </c:choose>

    <hr/>


    <%--遍历--%>
    <%
        List<String> list = Arrays.asList("A","B","C","D");
        request.setAttribute("list", list);
    %>
    <c:forEach items="${requestScope.list}" var="i" varStatus="status">
        <h2>${i}&nbsp; ${status.index}&nbsp; ${status.count}&nbsp; ${status.count%2==0}&nbsp; ${status.first}&nbsp; ${status.last}&nbsp;</h2>
    </c:forEach>

    <hr/>

    <%--重写URL--%>
    <%
        String  newUrl = response.encodeRedirectURL(request.getContextPath()+"/index.jsp");
    %>
    <%=newUrl%>

    <a href="<%=response.encodeRedirectURL(request.getContextPath()+"/index.jsp")%>">重写URL跳转</a>
    <a href="<c:url context='${pageContext.request.contextPath}' value='/index.jsp'/>">重写URL跳转</a>

</body>
</html>
  • 禁用cookie才会url重写

posted @ 2020-11-05 01:03  雨中遐想  阅读(113)  评论(0编辑  收藏  举报