我正在尝试使用 Thymeleaf 创建一个简单的网页,其中动物具有一些属性(如名称、动物类型和平衡)。然而,我一直受到正确语法的困扰。
我的Java代码是:
@GetMapping("/multipleaccounts")
public String multiple(Model model) {
List bankAccounts = new ArrayList<>();
model.addAttribute("account", bankAccounts);
return "multiple";
}
我的 thymeleaf 代码在/multipleaccounts 端点处的有问题的部分:
<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>`
最佳答案
我建议您不要使用原始类型,而是使用 List<Account>
或类似的。
迭代List
在 Thymeleaf 模板中,请参阅 Baeldung's webpage这个问题在哪里得到了正确的解释。
简而言之,字符串连接的正确表示法如下:
<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>
顺便问一下,您确定每个帐户都有 animalType
?
关于java - Thymeleaf - 在 JAVA 中传递模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53743118/