jstl中c:foreach的使用

来源:互联网 发布:字典破解wifi软件 编辑:程序博客网 时间:2024/06/01 14:38

1. 简单的遍历(后台传一个list过来)

1、后台传的list里放的是一个实体类对象,则可以直接以迭代器名.对象名 的形式遍历:

                       <select id="zyId" name="zyId">                            <option value="-1">请选择专业</option>                            <c:forEach items="${majorList}" var="majorList">                                <option value="${majorList.id}">${majorList.zymc}</option>                            </c:forEach>                        </select>

2、后台传的list里放的是map对象,则可以直接以迭代器名['key'] 的形式遍历:

            <c:forEach items="${wtList}" var="wtList">                <li data-id="${wtList['zy_id']}">                    <span>${wtList['zymc']}</span>                    <i>${wtList['wei']}/${wtList['yi']}</i>                </li>            </c:forEach>

2. 遍历时做判断(后台传一个list过来)

语法:

                  <c:forEach var="迭代器名字"   items="要迭代的list"   varStatus="每个对象的状态"                           begin="循环从哪儿开始"    end="循环到哪儿结束"    step="循环的步长">                             输出的内容                  </c:forEach>

1、集合判空,利用关键字 empty

                            <c:if test="${ empty getHasFinishedWt}">                                <div class="item active">                                    <p class="ticker-headline">                                        <a href="#" style="color: #D5D6E2">                                            没有查询到已完成的问题!                                        </a>                                    </p>                                </div>                            </c:if>

2、 C:forEach +C:if 的简单用法

<c:foreach items="${list}" var ="li" varStatus="status"> <c:if test="${status.first}">${status.current.id}</c:if> <c:if test="${status.last}">${status.current.name}</c:if> </c:foreach> 

3、 C:forEach +C:when 的简单用法

 <c:forEach items="${getHasFinishedWt}" var="nf" varStatus="status">    <c:choose>        <c:when test="${status.first}">         <div class="item active">          <p class="ticker-headline">           <a href="#" style="color: #D5D6E2">            ${status.current.sjjjsj} <strong>${status.current.zymc}</strong>                 专业的问题已解决:${status.current.wtsm}</a></p>         </div>        </c:when>        <c:otherwise >         <div class="item " >           <p class="ticker-headline">                <a href="#" style="color: #D5D6E2">${nf.sjjjsj}<strong>${nf.zymc</strong>                 专业的问题已解决:${nf.wtsm}</a></p>         </div>       </c:otherwise>   </c:choose></c:forEach>

4、循环遍历,按指定步长输出

<c:foreach items="list" var ="li" step="2">${li.id}</c:foreach>注意:step为循环的步长,每次隔两个单位输出一个。如:135...

注意:

<c:choose><c:when><c:otherwise>标签的使用必须符合以下语法规则:<c:when><c:otherwise>不能单独使用,它们必须位于<c:choose>父标签中。在<c:choose>标签中可以包含一个或多个<c:when>标签。在<c:choose>标签中可以不包含<c:otherwise>标签。在<c:choose>标签中如果同时包含<c:when><c:otherwise>标签,那么<c:otherwise>必须位于<c:when>标签之后。

如果后台传过来的list中有n条数据:1、当使用<c:if test="${status.first}">以特定样式输出数据时,只是把第一条数据额外的以特地的样式输出,然后再把所有样式以普通样式输出,总共输出n+1条数据;2、当使用 <c:when test="${status.first}">以特定样式输出数据时,会把第一条数据以特地的样式输出,然后从第二条数据以普通样式输出,总共输出n条数据;(所以上例子中我选择 C:forEach +C:when ,因为只想把第一条数据以特地样式输出)
原创粉丝点击