当我要为每个i.index设置值时遇到问题表java.lang.NumberFormatException:对于输入字符串:“ $ {i.index}”。在数组中,我需要数字,因此$ {i.index}是整数。我不知道我做错了什么。
<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>
答案 0 :(得分:2)
如果不使用预处理程序,则无法嵌套表达式(*{...${...}...}
)。您的代码应如下所示:
<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,因此必须使用预处理。)