【问题标题】:thymeleaf used in Spring, adding a list to iterate over with th:each在 Spring 中使用 thymeleaf,添加一个列表以使用 th:each 进行迭代
【发布时间】:2015-10-25 15:05:31
【问题描述】:

我想使用 th:each 构造来创建一个项目表。我在将数据添加到模型时遇到问题。我使用了这个结构:

        model.addAllAttributes(repository.findByusername(username));

findByusername 返回一个 java.Util.List 对象。然后我总是只有模型列表中的最后一个元素。我猜这取决于属性 namn 是类名,因此列表中每次添加新元素都会覆盖先前添加的元素。

如何将每个元素作为单独的属性添加到模型中,然后使用 th:each 轻松迭代它们?

【问题讨论】:

    标签: spring model thymeleaf


    【解决方案1】:

    你的控制器应该是返回,

    model.addAttribute("usersList", repository.findByusername(username)); 
    

    你的百里香代码应该是这样的,

      <th:block th:each="user : ${usersList}">
          <td th:text="${user.username}">Test</td>
      </th:block>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-10
      • 2021-04-24
      • 2020-10-09
      • 2018-01-07
      • 1970-01-01
      • 2021-06-10
      • 2023-01-02
      • 2014-04-11
      相关资源
      最近更新 更多