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

发布于 2025-01-20 14:32:40 字数 927 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2025-01-27 14:32:40

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

<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#预处理

You can use the iter.index using 2 underscores.

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

__${...}__ 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文