【问题标题】:Thymeleaf table百里香表
【发布时间】:2026-01-05 00:20:09
【问题描述】:

当我想为每个 i.index 设置值时出现问题表,得到错误 java.lang.NumberFormatException:对于输入字符串:“${i.index}”。在数组中我需要数字,所以 ${i.index} 是 int。我不知道我做错了什么。

<div class="form-group row" th:each="attribute, i: ${attributeList}">
        <label class="col-sm-3 col-form-label" th:text="${attribute.name}"></label>
        <div class="col-sm-9">
            <input type="text" th:field="*{technicalAttributes[${i.index}].name}" class="form- 
            control" placeholder="Nazwa">
        </div>
</div>

【问题讨论】:

    标签: spring-boot thymeleaf


    【解决方案1】:

    如果不使用预处理,您将无法嵌套表达式 (*{...${...}...})。您的代码应如下所示:

    <div class="form-group row" th:each="attribute, i: ${attributeList}">
      <label class="col-sm-3 col-form-label" th:text="${attribute.name}"></label>
      <div class="col-sm-9">
        <input type="text" th:field="*{technicalAttributes[__${i.index}__].name}" class="form-control" placeholder="Nazwa">
      </div>
    </div>
    

    (如果您没有使用 th:field 属性,则表达式 *{technicalAttributes[i.index].name} 也是合适的。但由于您使用的是 th:field,因此您必须使用预处理。)

    【讨论】: