S
S
Sland Show2018-09-20 18:19:53
Java
Sland Show, 2018-09-20 18:19:53

How can I display a valid table in JSP?

I have a matrix (List which consists of lists)

List<List<SeatDTO>> carriages = new ArrayList<List<SeatDTO>>();
        for (int carriagesIterator = 0; carriagesIterator < selectedSchedule.getTrain().getCarriages(); carriagesIterator++) {
            ArrayList<SeatDTO> seats = new ArrayList<SeatDTO>();
            for (int seatsIterator = 0; seatsIterator < selectedSchedule.getTrain().getSeats().size(); seatsIterator++) {
                seats.add(new SeatDTO(carriagesIterator, seatsIterator));
            }

            carriages.add(seats);
        }

        model.addAttribute("carriages", carriages);


SeatDTO contains information about the seat (seat number and car number). I need to write a table in JSP that will display all the seats for each car. It usually starts like this for me:
<table class="table table-striped">

            <tr>
                <th>Carriage</th>
            </tr>

            <!-- Loop over and print stations  -->
            <c:forEach var="tmpCarriage" items="${carriages}">
                <tr>
                    ...
                </tr>
            </c:forEach>

        </table>


But there's something I can't figure out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-09-20
@SlandShow

You just need to add a nested loop and output some value from SeatDTO

<c:forEach var="row" items="${carriages}">
    <tr>
      <c:forEach var="cell" items="${row}">
        <td>
            <c:out value="${cell.someAccessor}">
        </td>
      </c:forEach>
    </tr>
</c:forEach>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question