Reputation: 678
I am trying to create a simple webpage, in which animals have some properties (like name, animalType, and balance) using Thymeleaf. However, I've been suffering from the proper syntax.
My Java code is:
@GetMapping("/multipleaccounts")
public String multiple(Model model) {
List bankAccounts = new ArrayList<>();
model.addAttribute("account", bankAccounts);
return "multiple";
}
The problematic part of my thymeleaf code at the /multipleaccounts endpoint:
<div th:each="element : ${account}" th:object="${element}">
<p th:text="|My name is *{name}|"></p>
<p th:text="|My balance is *{balance}|"></p>
<p th:text="|My type is *{animalType}|"></p>
</div>`
Upvotes: 1
Views: 66
Reputation: 4643
This will helps you:
<div th:each="element : ${account}">
<p th:text="'|My name is ' + ${element.name} + '|'"></p>
<p th:text="'|My balance is ' + ${element.balance} + '|'"></p>
<p th:text="'|My type is ' + ${element.animalType} + '|'"></p>
</div>
Upvotes: 1
Reputation: 44398
I recommend you not use the raw types but rather List<Account>
or similar.
To iterate the List
in Thymeleaf template, please see the Baeldung's webpage where is this issue properly explained.
In a nutshell, the correct notation with the string concatenation is the following:
<div th:each="a: ${account}">
<p th:text="'|My name is ' + ${a.name} + '|'"></p>
<p th:text="'|My balance is ' + ${a.balance} + '|'"></p>
<p th:text="|My type is ' + ${a.animalType} + '|'"></p>
</div>
By the way, are you sure that each account has animalType
?
Upvotes: 1