Use the fn:length() EL function to calculate the total number of recipes. Before using any EL function , we also need to import the necessary tag library .
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Then we use <c:set> to set the common value as an attribute with the page area.
<c:set var="totalRecipes" value="${fn:length(recipes)}" />
<c:forEach> allows you to get the loop counter using its varStatus attribute. The counter area is local to the loop, and it automatically expands for you. This loop counter starts at 1.
<c:forEach var="recipe" items='${recipes}' varStatus="recipeCounter"> <c:if test="${recipeCounter.count > (totalRecipes - 3)}"> <div class="span4"> <h3<c:out value="${recipe.inputDescProb}"></c:out></h3> <p><c:out value="${recipe.inputDescSol}"></c:out></p> <p><a class="btn" href="/recipes/${recipe.id}">Details »</a></p> </div> </c:if> </c:forEach>
EDIT . Use the count property of the LoopTagStatus class to access the current iteration counter value in your EL element as ${varStatusVar.count} .
source share