c:forEach的varStatus属性

本文详细介绍了如何利用JSP的c:forEach标签结合varStatus属性来实现数据分组并根据分组进行样式交替,具体展示了偶数行与奇数行颜色的动态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c:forEach的varStatus属性

index当前这次迭代从 0 开始的迭代索引 
count当前这次迭代从 1 开始的迭代计数 
first用来表明当前这轮迭代是否为第一次迭代的标志 
last用来表明当前这轮迭代是否为最后一次迭代的标志 
begin属性值 
end属性值 
step属性值

例:

表格偶数行与奇数行颜色交替效果
引标签库
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<c:forEach items="${queryPromotionList}" var="vPromotion" varStatus="vstatus">
            <c:choose>
                <c:when test="${vstatus.index%2==0}">
      <tr bgcolor="#FFFFFF" height="40">
                </c:when>
                <c:otherwise>
        <tr bgcolor="#F3F3F5" height="40">
                </c:otherwise>
            </c:choose>

 

   <table class=table_body_bg cellspacing=1 cellpadding=1
            width="100%" align=center border=0>
             <c:forEach items="${list}" var="a" varStatus="vs">

     <c:if test="${vs.count%5==1}">
       <tr align="left" height="20">
      </c:if>             
             <td class=table_body_td width="20%"><a href="/aam/degree/advisorAnswer.do?sfid=${a.sfid }">${a.xm }(${a.sfid })</a></td>
     <c:set var="count" value="${vs.count}"/>         //${vs.count}只在<c:forEach></c:forEach>的范围内有值 外部引用需要把值传出去
     </c:forEach>    
      <c:if test="${count%5==1}">
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>
      </tr>
      </c:if> 
    
      <c:if test="${count%5==2}">
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>    
      </tr>
      </c:if> 
     
      <c:if test="${count%5==3}">
      <td class="table_body_td" width="20%"></td>
      <td class="table_body_td" width="20%"></td>
      </tr>
      </c:if> 
     
      <c:if test="${count%5==4}">
      <td class="table_body_td" width="20%"></td>
      </tr>
      </c:if>

      <c:if test="${count%5==0}">
      </tr>
      </c:if> 
             </table>

 

 

 

不论是对整数还是对集合进行迭代, <c:forEach> 剩余的属性 varStatus 所起的作用相同。和 var 属性一样, varStatus 用于创建限定了作用域的变量。不过,由 varStatus属性命名的变量并不存储当前索引值或当前元素,而是赋予 javax.servlet.jsp.jstl.core.LoopTagStatus 类的实例。该类定义了一组特性,它们描述了迭代的当前状态,下面列出了这些特性:

 

特性Getter描述
currentgetCurrent()当前这次迭代的(集合中的)项
indexgetIndex()当前这次迭代从 0 开始的迭代索引
countgetCount()当前这次迭代从 1 开始的迭代计数
firstisFirst()用来表明当前这轮迭代是否为第一次迭代的标志
lastisLast()用来表明当前这轮迭代是否为最后一次迭代的标志
begingetBegin()begin 属性值
endgetEnd()end 属性值
stepgetStep()step 属性值

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值