2012-05-11 12 views
0

コードの最後の項目をチェックするが、最後の2番目の項目をチェックする必要がありますが、これを達成するためにカウンタを使用できますが、より良い方法がありますか?最後の2番目の項目を確認する

<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と比較することです。