【问题标题】:thymeleaf show specific user profilethymeleaf 显示特定的用户资料
【发布时间】:2016-05-06 12:07:03
【问题描述】:

我是 thymeleaf 和 spring boot 的新手,所以我真的不知道我在寻找什么来解决我的问题。 我的网站上的列表中显示了几个用户。当我点击其中一个用户时,我想显示用户个人资料页面。

列表如下所示:

<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>

这是我的控制器:

@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";
}

在个人资料页面上,我使用 currentPerson.firstName 等来获取有关用户的信息。 所以我的问题是,如何将 currentPerson 更改为列表中最后点击的人?还是我做错了?

【问题讨论】:

    标签: spring-boot thymeleaf user-profile


    【解决方案1】:

    您需要有 2 个处理程序:一个用于显示您的列表,一个用于获取详细信息。前者不应该在模型中设置currentPerson,因为你还没有这个信息。

    您的处理程序应如下所示:

    @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";
    }
    

    (注意@RequestParameter的使用)

    我假设您的 idlong,并且它是强制性的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2012-04-24
      • 1970-01-01
      • 2021-03-09
      • 2013-05-12
      • 2019-08-09
      • 2019-09-18
      相关资源
      最近更新 更多