Search code examples
jspforeachhtml-tablejstl

How to print a List<T> as a <table> with 5 columns using <c:forEach>


My goal is to display my data in database just like in the image below.

enter image description here

I am using JSTL <c:forEach>.

<c:forEach items="${table}" var="table">
    <img height="70" src="${table.images}" />
    Table ${table.tableId}
</c:forEach>

How can I print a <table> with 5 columns?


Solution

  • Something like:

    <table>
      <c:forEach items="${list}" var="row" varStatus="rowCounter">
        <c:if test="${rowCounter.count % 5 == 1}">
          <tr>
        </c:if>
        <td><img src="${row.imageUrl}"/><br/>Table ${rowCounter.count}</td>
        <c:if test="${rowCounter.count % 5 == 0||rowCounter.count == fn:length(values)}">
          </tr>
        </c:if>
      </c:forEach >
    </table>