Mount a table of products as a window in java jsp

0

I'm having a hard time presenting a list of showcase-type products using Java and JSP .

I can get the data, but I can not fill this table using <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 it to look like this:

**produto 1 produto 2 produto 3**
**produto 4 produto 5 produto 6**
    
asked by anonymous 05.12.2017 / 02:13

1 answer

0

To write 3 in 3 products you can do something like this:

Use varStatus counter, for example contador , to know what forEach index, so every time contador.count is divisible by 3 you close the current line and add a new line in 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>

At the end you can end up with a% w / w% more, leaving your HTML inconsistent, to resolve this situation do a <tr> check before writing the new c:if , you should check if the counter is less than the size of the list. As I do not know if you are using array or list you can check the size with <tr> and .length() respectively.

    
05.12.2017 / 02:38