【问题标题】:can't reference iteration variable in Thymeleaf无法在 Thymeleaf 中引用迭代变量
【发布时间】:2015-05-08 13:54:47
【问题描述】:

我尝试使用th:each 迭代项目列表,

<div class="row" th:each="item : *{items}">
  <input type="text" th:field="item.name"/>
</div>

如果我使用th:text 访问迭代变量,它会起作用,但会抛出

java.lang.IllegalStateException: 既不是 BindingResult 也不是普通的 bean 名称“item”的目标对象可用作请求属性

当我使用th:field 访问它时,我哪里做错了?

【问题讨论】:

  • 你试过 th:each="item : ${items}" 吗?

标签: java spring thymeleaf


【解决方案1】:

这样的事情可能会奏效:

<div class="row" th:each="item, stat : *{items}">
  <input type="text" th:field="*{items[__${stat.index}__].name}"/>
</div>

在这里查看更多信息:http://forum.thymeleaf.org/I-have-problem-in-binding-the-list-of-objects-contained-inside-a-object-on-the-form-using-thymeleaf-td3525038.html

【讨论】:

  • 我知道这会起作用,但这很奇怪,因为你有一个迭代变量,但仍然必须使用索引来引用该项目。
【解决方案2】:

th:field 以多种方式被破坏,这是其中之一。

【讨论】:

    猜你喜欢
    • 2014-01-23
    • 2015-07-03
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    相关资源
    最近更新 更多