将对象列表解析为百日咳模板,并返回一个Object

时间:2016-02-23 10:36:27

标签: spring spring-mvc spring-boot thymeleaf

我在模型中添加了一个属性并返回页面。在admin.html页面中,我遍历所有用户并显示详细信息。但我想将一个用户从列表中返回"/admin/showReports"

我希望有人可以帮助我。我真的被困了。

控制器

@RequestMapping(value = "/admin")
public String showAllUser(Model model) {
    model.addAttribute("allUser",userDelegate.getAllUser());
    return "admin";
}

@RequestMapping(value = "/admin/showReports", method = RequestMethod.POST)
public String showAllAvailableReportsForUser(Model model, User user) {
    model.addAttribute("user", user);
    model.addAttribute("reports", userDelegate.getAllReports());
    return "addReports";
}

admin.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Admin</title>
    <link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.css"/>
    <script src="/webjars/bootstrap/3.3.6/webjars-requirejs.js"></script>
</head>
<body>
    <h1>Admin</h1>
    <div class="container">
        <div class="col-md-1"></div>
        <div class="panel panel-default col-md-10">
            <div class="panel-heading">User</div>
            <table class="table">
                <tr>
                    <th>Id</th>
                    <th>Username</th>
                    <th>E-mail</th>
                    <th>Reports</th>
                </tr>
                <tr th:each="user : ${allUser}">
                    <td th:text="${user.identifier}"></td>
                    <td th:text="${user.username}"></td>
                    <td th:text="${user.email}"></td>
                    <td th:text="${user.reports}"></td>
                    <form th:action="@{/admin/showReports}" method="post">
                        <td>
                            <!--Here is the Problem. How do i return one user from the list?-->
                            <input type="hidden" th:value="${user}" />
                            <button type="submit">edit</button>
                        </td>
                    </form>
                </tr>
            </table>
        </div>
        <div class="col-md-1"></div>
    </div>
</body>
</html>

ThymeleafConfig

@Bean
public SpringResourceTemplateResolver templateResolver(){
    SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setPrefix("classpath:/templates/");
    resolver.setSuffix(".html");
    resolver.setTemplateMode("HTML5");
    resolver.setOrder(1);
    return resolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine engine = new SpringTemplateEngine();
    engine.setTemplateResolver(templateResolver());
    engine.addDialect(new LayoutDialect());
    return engine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver resolver = new ThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    return resolver;
}

1 个答案:

答案 0 :(得分:1)

您可以通过网址传递唯一标识符来解决问题。

通过id的示例,请注意对GET的更改,因此只需放置一个链接而不是表单。

@RequestMapping(value = "/admin/showReports/{id}", method = RequestMethod.GET)
public String showAllAvailableReportsForUser(Model model, @PathVariable("id") Long id) {
    User user = userDelegate.getUserById(id); // Get user via id
    mav.addObject("user", user);
    // Get and add whatever else you need
    return "addReports";
}