zoukankan      html  css  js  c++  java
  • jstl-将List中的数据展示到表格中

    功能:

    使用jstl将List中的数据动态展示到Jsp表格中,并实现隔行换色功能。

    效果图:

    Jsp代码:

    <%@ page import="java.util.ArrayList" %>
    <%@ page import="java.util.List" %>
    <%@ page import="cn.ytmj.el.User" %>
    <%@ page import="java.util.Date" %><%--
      Created by IntelliJ IDEA.
      User: ada
      Date: 2019/8/15
      Time: 20:39
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <html>
        <head>
            <title>显示数据到表格中</title>
            <style type="text/css">
                .bor{
                    background-color: darkgray;
                }
            </style>
        </head>
        <body>
            <%
                Date date = new Date();
                List list = new ArrayList();
                list.add(new User("张三", 21, date));
                list.add(new User("张三", 21, date));
                list.add(new User("李四", 22, date));
                list.add(new User("王五", 23, date));
                list.add(new User("小名", 24, date));
                list.add(new User("李华", 25, date));
                request.setAttribute("list", list);
            %>
            <table border="1" align="center">
                <tr>
                    <th>编号</th>
                    <th>姓名</th>
                    <th>性别</th>
                    <th>日期</th>
                </tr>
                <c:forEach items="${list}" var="i" varStatus="s">
                    <c:if test="${s.count%2==0}">
                    <tr class="bor">
                        <td>
                                ${s.count}
                        </td>
                        <td>
                                ${i.name}
                        </td>
                        <td>
                                ${i.age}
                        </td>
                        <td>
                                ${i.simDate}
                        </td>
                    </tr>
                    </c:if>
                    <c:if test="${s.count%2==1}">
                        <tr >
                            <td>
                                    ${s.count}
                            </td>
                            <td>
                                    ${i.name}
                            </td>
                            <td>
                                    ${i.age}
                            </td>
                            <td>
                                    ${i.simDate}
                            </td>
                        </tr>
                    </c:if>
                </c:forEach>
    
    
            </table>
    
        </body>
    </html>
    
    

    Java代码:

    package cn.ytmj.el;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    /**
     * @author rui
     * @create 2019-08-14 21:53
     */
    public class User {
        private String name;
        private Integer age;
        private Date date;
    
        public User(String name, Integer age, Date date) {
            this.name = name;
            this.age = age;
            this.date = date;
    
        }
        //格式化时间
        public String getSimDate(){
            SimpleDateFormat sdf=new SimpleDateFormat("yy-MM-dd HH:mm:ss");
            String format = sdf.format(date);
            return format;
        }
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
        public User(){
    
        }
    }
    
    
    
  • 相关阅读:
    html5 存储方式
    分割字符串得到分数,然后求和取整
    通过javascript的日期对象来得到当前的日期
    基础选择器
    制作3D旋转视频展示区
    自由缩放属性resize
    团队项目第四天
    团队项目第三天
    团队项目第二天
    团队项目第一天
  • 原文地址:https://www.cnblogs.com/PoetryAndYou/p/11360643.html
Copyright © 2011-2022 走看看