Thymelaf th:field预处理不适用于th:each



我想在表中显示一个对象列表,以及一个更新字段marketallcoationlive的选项。我用的是百里香。因此,我不得不将th:eachth:field中可用的预处理能力结合使用。

在我的控制器类中,我设置了如下所示的属性:

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]

根据文档,如果使用预处理功能,则无需th:object。我没有用过,所以我不确定我在这里缺了什么。

  1. 如果使用*{...}语法和/或th:field属性,则必须使用th:object(它们都取决于th:object的功能(
  2. 您不应该在<span/>元素上使用th:field——这没有意义。(th:field设置元素的nameidvalue属性。namevalue不影响<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>

最新更新