Assemble a product table as a java jsp showcase

Asked

Viewed 257 times

0

I’m having a hard time presenting a showcase product list using Java and JSP.

I can get the data, but I don’t know how to fill this table using the <c:forEach>

My table would have this structure:

<table>
    <c:forEach items="${produtos}" var="p">
        <tr>
            <td> ${p.nome } </td> <td>${p.nome } </td> <td>${p.nome }</td>
        </tr>
    </c:forEach>
</table>

But what is displayed is:

**produto 1 produto 1 produto 1**
**produto 2 produto 2 produto 2**
**produto 3 produto 3 produto 3**

I wanted you to show off like this:

**produto 1 produto 2 produto 3**
**produto 4 produto 5 produto 6**

1 answer

0


To write from 3 to 3 products you can do something in this sense:

Use an accountant varStatus, for example contador, to find out what the forEach, this way every time contador.count is divisible by 3 you close the current row and add a new row in the table.

<table>
    <c:forEach items="${produtos}" var="p" varStatus="contador">
        <tr>
            <td> ${p.nome } </td>
        <c:if test="${contador.count % 3}">
        </tr>
        <tr>
        </c:if>
    </c:forEach>
</table>

In the end you can end up with a <tr> further, leaving your HTML inconsistent, to resolve this situation make a check c:if before writing the new <tr>, You should check if the counter is smaller than the list size. As I don’t know if you are using array or list you can check the size with .length() and .size() respectively.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.