Search code examples
jspjstl

How to stop showing duplicated items in jsp


To jsp i send list of Routes

request.setAttribute(ROUTES, routeService.getAll());

Routes has Train->List<Wagon> Wagon has type (Business, Standart, etc) and count of seats.

I need to show for every Route:

Business: 1313,
Standart: 131...

I need to summ count of seats of each Wagon of all types and show it in JSP for each Route.

How to achieve this via jstl.


Solution

  • You should do your logic in your servlet and send it to jsp. Also you can write java codes in jsp which I suggest you to avoid. To Solve your problem you need a code like this in your servlet:

    Map<String, Int> trainMap = trainList.stream().collect(Collectors.groupingBy(Wagon::getType,
        Collectors.summingInt(Wagon::getCount)));
    request.setAttribute("trainMap", trainMap);
    

    And in your jsp do something like this:

    <c:forEach var="train" items="${trainMap}">
        <tr>
            <td>
            <c:out value="${train.key}" /> : <c:out value="${train.value}" />
            </td>
         </tr>
    </c:forEach>