<c:forEach varStatus="status">中 varStatus的作用

varStatus是<c:forEach>jstl循环标签的一个属性,varStatus属性。

varStatus=“status”事实上定义了一个status名的对象作为varStatus的绑定值。

该绑定值也就是status封装了当前遍历的状态,比如,可以从该对象上查看是遍历到了第几个元素:${status.count}

<span style="color: red; font-size: 20px;">${status.count}</span> 写在 c:forEach 里面.

常见的用法的是<c:forEach var="e" items="${ss.list}" varStatus="status">
                                 <!--实现隔行变色效果--> 
                                 <c:if test="${status.count%2==0}" >
                                       <tr  style="color: red; font-size: 20px;"/>
                                  </c:if>

                                  <c:if test="${status.count%2!=0}" >
                                       <tr  style="color: blue; font-size: 20px;"/>
                                  </c:if> 

                        </c:forEach>

     <c:forEach items="${students}" var="s" varStatus="vs">    
         <tr style='background-color: ${vs.count %2==0 ?"blue":"gray"};'>
             <td>${s.id }</td>
             <td>${s.name }</td>
             <td>${s.age }</td>
         </tr>
    </c:forEach>

分行显示蓝色或则灰色。

原文地址:https://www.cnblogs.com/jianshuai520/p/9473041.html