简体   繁体   English

Thymeleaf-JAVA中的传递模型

[英]Thymeleaf - passing models in JAVA

I am trying to create a simple webpage, in which animals have some properties (like name, animalType, and balance) using Thymeleaf. 我正在尝试创建一个简单的网页,其中使用Thymeleaf使动物具有一些属性(例如名称,animalType和balance)。 However, I've been suffering from the proper syntax. 但是,我一直在使用正确的语法。

My Java code is: 我的Java代码是:

   @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: 我的百里香代码在/ 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>`

I recommend you not use the raw types but rather List<Account> or similar. 我建议您不要使用原始类型,而应使用List<Account>或类似的类型。

To iterate the List in Thymeleaf template, please see the Baeldung's webpage where is this issue properly explained. 要迭代Thymeleaf中的List模板,请参阅Baeldung的网页 ,该问题已在此处正确解释。

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 ? 顺便说一句,您确定每个帐户都具有animalType吗?

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM