How to skip the first element of a list in c: forEach

I am using <c:forEach>

to iterate over List

as below:

<c:forEach items="${list}" var="item">
    ${item}
</c:forEach>

      

How do I skip printing the first list item?

+3


source to share


2 answers


You can use an attribute varStatus

to get the status of the iteration , which in turn has, among others isFirst()

, which you could check in a block <c:if>

.



<c:forEach items="${list}" var="item" varStatus="loop">
    <c:if test="${not loop.first}">
        ${item}
    </c:if>
</c:forEach>

      

+1


source


Use varStatus

in the test forEach

andif

<c:forEach items="${list}" var="item" varStatus="state">
    <c:if test="${not state.first}">
       ${item}
    </c:if>
</c:forEach>

      



Other useful properties of varStatus :

  • current

    Item (from collection) for the current round of iteration
  • index

    Zero-indexed index for the current round of iteration
  • count

    A unit counter for the current round of iteration
  • first

    Flag indicating whether the current round is the first pass through the iteration
  • last

    Flag indicating whether the current round is the last pass through the iteration
  • begin

    Begin attribute value
  • end

    End attribute value
  • step

    The value of the step attribute
+5


source







All Articles