Jstl核心标签库

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>    
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        pageContext.setAttribute("user", "admin");
    %>
    <!-- c:out标签 -->
    el表达式:${user }<br>
    c:out标签:<c:out value="${user }"></c:out><br>
    
    <!-- c:catch -->
    <c:catch    var="e">
        ${pageContext.name }
    </c:catch>
    ${e.message }<br>
    
    <!-- c:if -->
    <c:if test="${user== 'admin'  }" var="flag" >
        欢迎登陆<br>
        ${flag }
    </c:if>
    
    <!-- c:choose -->
    <%
        pageContext.setAttribute("hobby", "basketball");
    %>
    
    <c:choose>
        <c:when test="${hobby=='basketball' }">
            我喜欢打篮球
        </c:when>
        <c:when test="${hobby == 'football' }">
            我喜欢踢足球
        </c:when>
        <c:otherwise>
            我啥都不喜欢
        </c:otherwise>
    </c:choose>
    
    <!-- c:foreach -->
    <br>---------------------遍历数组---------------------<br>
    <%
        Object[] city ={"北京","天津","上海","深圳"};
        pageContext.setAttribute("city", city);
    %>
    <c:forEach items="${city }" var="c">
        ${c }<br>
    </c:forEach>
    
    <br>---------------------遍历list---------------------<br>
    <%
        List<String> s = new ArrayList<>();
        s.add("路飞");
        s.add("索隆");
        s.add("娜美");
        pageContext.setAttribute("s", s);
    %>
    <c:forEach items="${s }" var="s">
        ${s }<br>
    </c:forEach>
    
    <br>---------------------遍历set---------------------<br>
    <%
        Set<String> set = new HashSet<>();
        set.add("路飞");
        set.add("索隆");
        pageContext.setAttribute("set", set);
    %>
    <c:forEach items="${set }" var="set">
        ${set }<br>
    </c:forEach>
    <br>---------------------遍历map---------------------<br>
    <%
        Map<String,String> map = new HashMap<>();
        map.put("船长", "路飞");
        map.put("战斗员", "索隆");
        map.put("航海员", "娜美");
        map.put("狙击手", "乌索普");
        map.put("山治", "厨师");
        map.put("医生", "乔巴");
        map.put("历史学者", "罗宾");
        map.put("造船工", "弗兰奇");
        map.put("音乐家", "布鲁克");
        map.put("舵手", "甚平");
        pageContext.setAttribute("map", map);
    %>
    <c:forEach items="${map }" var="m">
        ${m.key }:${m.value }
        <br>
    </c:forEach>
    <br>------------------指定遍历的起始索引-----------------<br>
    <%
        Object[] country = {"中国","美国","德国","英国"};
        pageContext.setAttribute("country", country);
    %>
    <c:forEach items="${country }" var="count" begin="1" end="3" step="2">
        ${count }
    </c:forEach>
    <!-- varStatus -->
    <%
        Map<Integer,String> string = new HashMap<>(); 
        string.put(1,"luffy");
        string.put(2,"zoro");
        string.put(3,"name");
        pageContext.setAttribute("string", string); 
    %>
    <table border="1">
        <tr >
            <td>序号</td>
            <td>学号</td>
            <td>姓名</td>
        </tr>
        <c:forEach items="${string }" var="s" varStatus="obj">
            <tr style="background-color:${obj.index % 2 ==0? 'orange' :'red'}">
                <td>${obj.count }</td>
                <td>${s.key }</td>
                <td>${s.value }</td>
            </tr>
        </c:forEach>
    </table>
</body>
</html>

 

posted on 2018-09-11 11:28  董大志  阅读(109)  评论(0编辑  收藏  举报

导航