2012-05-11 5 views
0

아래의 코드는 마지막 항목을 검사하지만 두 번째 마지막 항목을 확인해야하는데이를 위해 카운터를 사용할 수 있지만 더 좋은 방법이 있습니까?두 번째 마지막 항목 확인하기

<c:if test="${status.last}"> 

답변

0

JacobM은 올바른 생각을 가지고 있지만, 나는 이것이 더 비슷하다고 생각합니다.

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<c:forEach var="current" varStatus="status" items="${list}"> 
    <c:if test="${(fn:length(list) - 1) == status.count}" > 
     The next to last item is ${current} 
    </c:if> 
</c:forEach> 

또는

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 
<c:set var="nextToLast" value="${fn:length(list) - 1}" /> 
<c:forEach var="current" varStatus="status" items="${list}"> 
    <c:if test="${nextToLast == status.count}" > 
     The next to last item is ${current} 
    </c:if> 
</c:forEach> 
1

한 가지 방법이 코드는 모든 반복에있는 목록의 크기를 계산 것이라고

//looping through a list called "myList" 

<c:if test="${fn:length(myList)==(status.count+1)}"> 

참고; 보다 나은 접근 방법은 크기를 변수에 넣고 그 값을 status.count와 비교하는 것입니다.

관련 문제