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
01234
56789
1011121314
1516171819
202122
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值