如何使用 thymeleaf 将迭代索引放入数组索引?

How to place iteration index into array index using thymeleaf?

我正在尝试通过跟踪迭代来迭代列表,我想将迭代索引放入该列表的数组索引中:

我尝试了什么:

<tr th:each="experience, iter : *{experiences}">
    <td>Experience name:</td>
    <td><input type="text" th:field="*{experiences[iter.index]}" value="${experience.name}">
</tr>

这里我得到 NumberFormatException。所以我尝试使用$,因为我认为iter是一个变量:

<tr th:each="experience, iter : *{experiences}">
    <td>Experience name:</td>
    <td><input type="text" th:field="*{experiences[${iter.index}]}" value="${experience.name}">
</tr>

在这里,${iter.index} 没有被评估,所以我得到错误 ${iter.index} 不是一个数字(再次 NumberFormatException)。

您可以使用 iter.index 使用 2 个下划线。

<tr th:each="experience, iter : *{experiences}">
    <td>Experience name:</td>
    <td><input type="text" th:field="*{experiences[__${iter.index}__]}" />
</tr>

__${...}__ 语法是一种预处理表达式,它是在实际计算整个表达式之前计算的内部表达式。 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#preprocessing