2010-11-29 5 views
0

J'ai le problème suivant (nouveau à jstl), besoin de créer 4 colonnes de produits dans un e-store, mais je ne sais pas comment itérer manuellement le tableau, a obtenu ce code qui répète le produit pour chaque ligne:jstl List itération manuelle?

<c:forEach items="${productCollection}" var="product"> 

     <tr> 
      <c:forEach var="i" begin="1" end="4"> 

       <td> 
        <a href="productDetail.htm"><img width="90" alt="${product.productName}" src="${product.productImage}"/></a> 

        <a href="productDetail.htm"><c:out value="${product.productName}" /></a> 

       </td> 

      </c:forEach> 

     </tr> 

    </c:forEach> 

J'ai besoin de déplacer manuellement la productLectionection arrayList sur la deuxième boucle. Je voudrais pouvoir faire quelque chose comme le Java Iterator:

<c:forEach items="${productCollection}" var="product"> 

     <tr> 
      <c:forEach var="i" begin="1" end="4"> 

       <td> 
        <a href="productDetail.htm"><img width="90" alt="${**Iterator.next().**product.productName}" src="${product.productImage}"/></a> 

        <a href="productDetail.htm"><c:out value="${**Iterator.next().**product.productName}" /></a> 

       </td> 

      </c:forEach> 

     </tr> 

    </c:forEach> 

Im désolé si c'est une question stupide, toute aide serait appréciée! Merci

+0

vous voulez un produit différent par colonne? que se passe-t-il s'il y a plus de 4 produits? –

Répondre

3

en supposant que vous voulez 4 produits par colonne, et que vous souhaitez envelopper à la ligne suivante s'il y a plus de 4 produits, vous pouvez utiliser un varStatus:

<tr> 
<c:forEach items="${productCollection}" var="product" varStatus="status"> 
    <c:if test="${status.index % 4 == 0 && !status.first && !status.last}"> 
     </tr> 
     <tr> 
    </c:if> 

    <td> 
     <a href="productDetail.htm"><img width="90" alt="${product.productName}" src="${product.productImage}"/></a> 
     <a href="productDetail.htm"><c:out value="${product.productName}" /></a> 
    </td> 
</c:forEach> 
</tr> 
+0

bien, que puis-je dire, parfait! Je vous remercie! – Ernest

+0

génial! de rien! :-) –