File size: 2,009 Bytes
08aee07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %> <!-- 打开EL指令 -->

<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="Javabean.User" %>

<html>
    <head>
        <title>JSTL案例</title>
    </head>
    <body>
        <!--
            需求:
            在request域中有一个存有User对象的List集合。需要使用jstl+el将list集合数据展示到jsp页面的表格table中
        -->

        <%
            List list = new ArrayList();
            list.add(new User("张三", 23, new Date()));
            list.add(new User("李四", 24, new Date()));
            list.add(new User("王五", 25, new Date()));

            request.setAttribute("list", list);
        %>

        <table align="center" border="1" width="50%">
            <tr>
                <th>编号</th>
                <th>姓名</th>
                <th>年龄</th>
                <th>生日</th>
            </tr>

            <c:forEach items="${list}" var="user" varStatus="s">
                <!-- 奇数行显示红色,偶数行显示绿色 -->

                <c:if test="${ s.count % 2 == 0 }">
                    <tr align="center" bgcolor="green">
                        <td>${ s.count }</td>
                        <td>${ user.name }</td>
                        <td>${ user.age }</td>
                        <td>${ user.birthStr }</td>
                    </tr>
                </c:if>

                <c:if test="${ s.count % 2 != 0 }">
                    <tr align="center" bgcolor="red">
                        <td>${ s.count }</td>
                        <td>${ user.name }</td>
                        <td>${ user.age }</td>
                        <td>${ user.birthStr }</td>
                    </tr>
                </c:if>

            </c:forEach>
        </table>
    </body>
</html>