Tartar
Tartar

Reputation: 5452

How to print data with Thymeleaf

I am new to Spring MVC and trying to list some data on the view by using Thymeleaf

Controller

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

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

        return "booksWithMultipleAuthors";
    }

View:

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

I am able to list books but when it comes to list authors i am getting 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) error. How do i fix this ?

Upvotes: 1

Views: 5581

Answers (1)

Lukehey
Lukehey

Reputation: 1354

The attribute name you are adding to the model does not match the object you are trying to access in your view.

Controller:

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

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

        return "booksWithMultipleAuthors";
    }

View:

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

Upvotes: 1

Related Questions