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(){
}
}