我想在表格中显示对象列表以及实时更新字段 marketallcoation
之一的选项。我用 thymeleaf 来达到这个目的。因此,我必须将 th:each
与 th:field
中可用的预处理能力结合使用。
在我的 Controller 类中,我设置了如下所示的属性:
model.addAttribute("marketList",supplyAllocationService.getItems());
在我的 html 页面中,我做了这样的事情:
<table>
<tr th:each="market,iteration : *{marketList}">
<td><span th:text="${market.date}" th:field="*{marketList[__${iteration.index}__].date}"> Date </span></td>
<td><span th:text="${market.country}" th:field="*{marketList[__${iteration.index}__].country}"> Country </span></td>
<td><span th:text="${market.product}" th:field="*{marketList[__${iteration.index}__].product}"> Product </span></td>
<td><span th:text="${market.sku != null} ? ${market.sku} : 'None'" th:field="*{marketList[__${iteration.index}__].sku}"> SKU </span></td>
<td><span th:text="${market.aggregateddemand}" th:field="*{marketList[__${iteration.index}__].aggregateddemand}"> Aggregated Demand </span></td>
<td><span th:text="${market.aggsupply_12}" th:field="*{marketList[__${iteration.index}__].aggsupply_12}"> 12 weeks aggregated supply </span></td>
<td><input type="text" th:value="${market.marketallcoation}" th:field="*{marketList[__${iteration.index}__].marketallcoation}"/></td>
<td><span th:text="${market.unmetdemand}" th:field="*{marketList[__${iteration.index}__].unmetdemand}"> Unmet demand quantity </span></td>
<td><span th:text="${market.demandplanner}" th:field="*{marketList[__${iteration.index}__].demandplanner}"> Demand Planner </span></td>
<td><span th:text="${market.bdmcontact}" th:field="*{marketList[__${iteration.index}__].bdmcontact}"> BDM contact </span></td>
<td></td>
</tr>
</table>
当我运行代码时,出现以下错误:
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'marketList[0]' available as request attribute
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:153) ~[spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
根据 docs ,如果使用预处理功能,则不需要th:object
。我没有使用过它,所以我不确定我在这里错过了什么。
最佳答案
- 如果您使用
*{...}
语法和/或th:field
属性,那么你必须使用th:object
(它们都依赖th:object
来发挥作用)。 - 您不应该使用
th:field
在<span/>
上元素——这没有意义。 (th:field
设置元素的name
、id
和value
属性。name
和value
不影响<span />
s。)
此外,不幸的是,我认为您不能使用 List
作为表单对象。因此,要修复您的表单,您需要首先创建一个具有 marketList
的新对象。作为它的属性之一,然后将其添加到模型中。使该新对象成为 th:object
如果您的表单符合您的要求,那么预处理应该适合您。
<form th:object="${yourNewObject}>
<table>
<tr th:each="market, iteration: *{marketList}">
<td th:text="${market.date}" />
<td th:text="${market.country}" />
<td th:text="${market.product}" />
<td th:text="${market.sku != null} ? ${market.sku} : 'None'" />
<td th:text="${market.aggregateddemand}" />
<td th:text="${market.aggsupply_12}" />
<td><input type="text" th:field="*{marketList[__${iteration.index}__].marketallcoation}"/></td>
<td th:text="${market.unmetdemand}" />
<td th:text="${market.demandplanner}" />
<td th:text="${market.bdmcontact}" />
<td></td>
</tr>
</table>
</form>
关于java - thymeleaf :field pre-processing is not working with th:each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60724440/