本文主要是介绍el foreach 的嵌套循环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
//创建一个list集合
List list = new ArrayList();
for (int i = 0; i < 23; i++) {
list.add(i);
}
//将上面的list集合分成多个集合l,然和在把分成的集合撞进一个集合的集合lists
//1:先根据list的对象个数算出分为多少行:
int rownum = 0;
if (list.size() % 5 == 0) {
rownum = list.size() / 5;
} else {
rownum = list.size() / 5 + 1;
}
List lists = new ArrayList();
for (int i = 0; i < rownum; i++) {
List l = new ArrayList();
for (int j = i * 5; j < i * 5 + 5 && j < list.size(); j++) {
l.add(list.get(j));
}
lists.add(l);
}
//把集合的集合lists存入session
session.setAttribute("lists", lists);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<style type="text/css">
tr.foreach_tr1 {
background-color: #D7E9FD;
color: black;
}
tr.foreach_tr2 {
background-color: #A7DBFF;
color: black;
}
</style>
</head>
<body>
<table border="1" width="200" cellpadding="0" cellspacing="0">
<tr align="center" class="first_tr">
<td colspan="5">
test
</td>
</tr>
<tr>
<c:forEach items="${lists}" varStatus="i" var="list">
<tr>
<c:forEach items="${list}" var="obj">
<td>
<c:out value="${obj}"></c:out>
</td>
</c:forEach>
</tr>
</c:forEach>
</tr>
</table>
</body>
</html>
运行的效果:
test | ||||
0 | 1 | 2 | 3 | 4 |
5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | ||
这篇关于el foreach 的嵌套循环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!