jstl中c:foreach的使用

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 ,因为只想把第一条数据以特地样式输出)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值