学无先后,达者为师

网站首页 前端文档 正文

JSTL:foreach标签对list集合的操作在jsp页面应用实例

作者:19Java菜鸟 更新时间: 2021-12-11 前端文档

JSTL:foreach标签对list集合的操作在jsp页面应用实例

jstl中foreach
item:el表达式的集合名
var:临时变量名
varstateus:循环状态对象
<%
    List<User> list = new ArrayList<>();
    list.add(new User(1, "张三", "123"));
    list.add(new User(2, "李四", "456"));
    list.add(new User(3, "王五", "789"));
    request.setAttribute("list", list);
%>
<table align="center" border="1">
    <tr>
        <th>
            id
        </th>
        <th>
            账号
        </th>
        <th>
            密码
        </th>
    </tr>
    <c:forEach items="${list}" var="user" varStatus="s">
        <c:if test="${s.count%2==0}">
            <tr bgcolor="blue">
                <td>
                        ${user.id}
                </td>
                <td>
                        ${user.username}
                </td>
                <td>
                        ${user.password}
                </td>
            </tr>
        </c:if>
        <c:if test="${s.count%2!=0}">
            <tr bgcolor="red">
                <td>
                        ${user.id}
                </td>
                <td>
                        ${user.username}
                </td>
                <td>
                        ${user.password}
                </td>
            </tr>
        </c:if>
    </c:forEach>
</table>

原文链接:https://blog.csdn.net/LongLiveThePRC/article/details/122162533

栏目分类
最近更新