您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
19_JSTL_练习(实现foreach遍历表格并隔行换色)
发布时间:2022-09-12 22:52:24编辑:雪饮阅读()
需求很简单,就是例如遍历出一个用户列表,然后隔行换色下就行。
那么首先之前用的User.java要增加一个构造函数,便于直接new出用户。
public User(String name, int age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
那么接下来本次的实例如foreach.jsp则如:
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.List" %>
<%@ page import="package1.User" %>
<%@ page import="java.util.Date" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/9/11
Time: 23: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</title>
</head>
<body>
<%
List list=new ArrayList();
list.add(new User("kasumi",24,new Date()));
list.add(new User("ayani",18,new Date()));
list.add(new User("moniji",28,new Date()));
list.add(new User("sarah bry ant",30,new Date()));
request.setAttribute("list",list);
%>
<!--数据行-->
<center>
<table border="1px">
<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 style="color:pink;">
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.birthday}</td>
</tr>
</c:if>
<c:if test="${s.count %2 !=0}">
<tr style="color:blue;">
<td>${s.count}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.birthday}</td>
</tr>
</c:if>
</c:forEach>
</table>
</center>
</body>
</html>
关键字词:JSTL,练习,实现,foreach,遍历,表格,隔行,换色