spring-bootthymeleafuser-profile

thymeleaf show specific user profile


Im new to thymeleaf and spring boot so I don't really know what I'm looking for to solve my problem. I have several users shown in a list on my website. When I click on one of the users, I want to display the users profile page.

The list looks like this:

<ul>
    <li th:each="section : ${sections}">
        <a href="overviewDivision.html?id=1" th:href="@{/overviewDivision?id=1}">
            <span th:text="${section.name}">Bereich3</span>
        </a>
        <ul>
            <li th:each="person : ${personSectionService.getPersonsBySectionId(section.id)}">
                <a href="overviewEmployee.html?id= + person.id" th:href="@{/overviewEmployee?id= + person.id}">
                    <span th:text="${person.firstName + ' ' + person.lastName}">
                    </span>
               </a>
           </li>
       </ul>
   </li>
</ul>

This is my controller:

@RequestMapping(value = "/overviewEmployee", method = RequestMethod.GET)
public String overviewEmployee(Model model) {
    model.addAttribute("sections", sectionService.getAllSections());
    model.addAttribute("personSectionService", personSectionService);
    model.addAttribute("currentPerson", personService.getById(1));
    return "overviewEmployee";
}

On the profile page I use currentPerson.firstName and so on to get the information about the user. So my question is, how do I change the currentPerson into the last clicked person in the list? Or am I doing it totally wrong?


Solution

  • You need to have 2 handlers for that: one to display your list, and one to get the detail. The former should not set the currentPerson in the model, because you don't have this information yet.

    Your handlers should look something like this:

    @RequestMapping(value = "/employees", method = RequestMethod.GET)
    public String overviewEmployee(Model model) {
        model.addAttribute("sections", sectionService.getAllSections());
        model.addAttribute("personSectionService", personSectionService);
        return "employee-list";
    }
    
    @RequestMapping(value = "/overviewEmployee", method = RequestMethod.GET)
    public String overviewEmployee(Model model, @RequestParameter long id) {
        model.addAttribute("currentPerson", personService.getById(id));
        return "overviewEmployee";
    }
    

    (Notice the use of @RequestParameter)

    I'm assuming your id is a long, and that it's mandatory.