如何使用Thymeleaf打印数据

时间:2015-12-13 13:39:55

标签: spring spring-mvc thymeleaf

我是Spring MVC的新手,并尝试使用Thymeleaf列出视图中的一些数据

控制器

   @RequestMapping("/booksWithMultipleAuthors")
    public String booksWithMultipleAuthors(Model model) {

        model.addAttribute("booksWithMultipleAuthors", this.userService.getAllWithAuthors());

        return "booksWithMultipleAuthors";
    }

查看:

<div th:each="author : *{authors}" th:object="${author}">
    <a th:text="${author.name}">Author name:</a>
</div>

我可以列出书籍,但是当涉及列表作者时,我收到HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.standard.processor.attr.StandardEachAttrProcessor' (booksWithMultipleAuthors)错误。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您要添加到模型的属性名称与您尝试在视图中访问的对象不匹配。

控制器:

@RequestMapping("/booksWithMultipleAuthors")
    public String booksWithMultipleAuthors(Model model) {

        model.addAttribute("authors", this.userService.getAllWithAuthors());

        return "booksWithMultipleAuthors";
    }

查看:

<div th:each="author : ${authors}">
    <a th:text="${author.name}">Author name:</a>
</div>