开发者

jstl List manual iteration?

开发者 https://www.devze.com 2023-01-27 16:50 出处:网络
i have the following problem (new to jstl), need to create 4 columns of products in a e-store, however dont know how to manually iterate trhough the array, got this code which repeats the product for

i have the following problem (new to jstl), need to create 4 columns of products in a e-store, however dont know how to manually iterate trhough the array, got this code which repeats the product for each row:

<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>

i need to move trhough the productColeection arrayList manually on the second loop. i wish i could do something like with the Java Iterator:

<c:for开发者_JAVA百科Each 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 sorry if its a dumb question, any help would be appreciated! Thanks


assuming you want 4 products per column, and then want to wrap to the next row if there's more than 4 products, you could use a 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号