Search code examples
sqlspringthymeleaf

Make a view of a linked table in Spring-Thymeleaf


I have trouble to make a view of linked table in Thymeleaf. In my database I have 3 tables: collaborators, languages, level and I have a linked table collab_laguages_level. In my repository class I have a query to show which languages for which collaborators.

enter image description here

In my controller I have my @RequestMapping:

enter image description here

But I have trouble to map it in my thymeleaf view. I've tried a lot of different things but nothing works. I want to create a particular profile for all the employees to show which languages (java, php, js...) they "speak".


Solution

  • I believe you are looking for a way to create a paginated view of your collaborators.

    Please check this tutorial for creating pagination with Thymeleaf and Spring.

    First you have to prepare the required pagination-information in your controller:

    model.addAttribute("list", x.getContent()); // your list
    int totalPages = list.getTotalPages();
        if (totalPages > 0) {
            List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
                .boxed()
                .collect(Collectors.toList());
            model.addAttribute("pageNumbers", pageNumbers); // list of all page-numbers (so users can click on the specific page)
        }
    

    Then in Thymeleaf you iterate over your list like this (simple example):

     <tr th:each="collaborator : ${list}">
                <td th:text="${collaborator.language}" />  // variable names of Collaborator entity
                <td th:text="${collaborator.languageLevel}" />
            </tr>
    

    And below you make your pagination (simple example):

    <div th:if="${list.totalPages > 0}" 
        th:each="pageNumber : ${pageNumbers}">
        <a th:href="@{/profiles(size=${list.size}, pages=${pageNumber})}"
            th:text=${pageNumber}></a>
    </div>