本帖参看其他文章,如有侵权与我联系
方法一:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
List<String> member = new ArrayList<String>();
member.add("hello");
member.add("world");
request.setAttribute("list", list);
for (int i = 0; i < list.size(); i++) {
request.setAttribute("option" + i, member);
}
%>
<c:forEach items="${list}" varStatus="status" var="item">
${item}的选项
<c:set value="${'option'}${status.index}" var="option1" />
<c:forEach items="${requestScope[option1]}" var="i">
${i}
</c:forEach>
<br>
</c: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>