如何使用 thymeleaf 将迭代索引放入数组索引中?
I am trying to iterate over list with keeping track of iteration and I want to place iteration index into array index of that list:
What I tried:
<tr th:each="experience, iter : *{experiences}">
<td>Experience name:</td>
<td><input type="text" th:field="*{experiences[iter.index]}" value="${experience.name}">
</tr>
Here I get NumberFormatException
. So I tried to use $
, because I think that iter
is a variable:
<tr th:each="experience, iter : *{experiences}">
<td>Experience name:</td>
<td><input type="text" th:field="*{experiences[${iter.index}]}" value="${experience.name}">
</tr>
Here, ${iter.index}
is not evaluated, so我会发现$ {iter.index}
不是数字的错误(再次numberformateXception
)。
I am trying to iterate over list with keeping track of iteration and I want to place iteration index into array index of that list:
What I tried:
<tr th:each="experience, iter : *{experiences}">
<td>Experience name:</td>
<td><input type="text" th:field="*{experiences[iter.index]}" value="${experience.name}">
</tr>
Here I get NumberFormatException
. So I tried to use $
, because I think that iter
is a variable:
<tr th:each="experience, iter : *{experiences}">
<td>Experience name:</td>
<td><input type="text" th:field="*{experiences[${iter.index}]}" value="${experience.name}">
</tr>
Here, ${iter.index}
is not evaluated, so I get error that ${iter.index}
is not a number (again NumberFormatException
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用2个下划线使用
iter.index
。__ $ {...} __
语法是一种预处理表达式,它是一个内部表达式,在实际评估整个表达式之前进行评估。 https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#预处理You can use the
iter.index
using 2 underscores.__${...}__
syntax is a preprocessing expression, which is an inner expression that is evaluated before actually evaluating the whole expression. https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf#preprocessing