Java Thymeleaf th:字段预处理不适用于th:每个
我想在表中显示一个对象列表,以及一个更新字段Java Thymeleaf th:字段预处理不适用于th:每个,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,我想在表中显示一个对象列表,以及一个更新字段marketallcoationlive的选项。我使用了百里香叶。因此,我必须将th:each与th:field中可用的预处理能力结合使用 在我的控制器类中,我设置了如下所示的属性: model.addAttribute("marketList",supplyAllocationService.getItems()); 在我的html页面中,我执行以下操作: <table> <tr th:each="market,iteration
marketallcoation
live的选项。我使用了百里香叶。因此,我必须将th:each
与th: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>
日期
国家
产品
SKU
总需求
12周总供应量
未满足需求量
需求计划员
BDM触点
运行代码时,出现以下错误:
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]
原因:java.lang.IllegalStateException:bean名称“marketList[0]”的BindingResult和普通目标对象都不能作为请求属性使用
在org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153)~[spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)~[spring-webmvc-5.1.6.RELEASE.jar:5.1.6.RELEASE]
位于org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
在org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
根据,如果使用预处理功能,则不需要th:object
。我没有用过,所以我不确定我在这里遗漏了什么
*{…}
语法和/或th:field
属性,则必须使用th:object
(它们都取决于th:object
的功能)
元素上使用th:field
——这没有意义。(th:field
设置元素的name
、id
和value
属性。name
和value
不影响
s。)列表
用作表单对象。因此,要修复表单,首先需要创建一个新对象,该对象的属性之一是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>
谢谢您的回复。我们能否使用表单提交而不使用隐藏元素来更新和持久化市场
对象?我已经接受了你的回答,因为它回答了我在帖子中的问题。但是,我确实有一个问题:“如何在单击提交按钮时提交特定的市场
对象?”。现在,我必须提交整个列表,循环遍历每个对象并更新值。是的,如果您在控制器上使用@SessionAttributes
设置模型对象,则无需使用隐藏元素即可完成此操作,这将仅为单个表单调用保留已设置的信息。至于你的第二个问题。是的,你可以这样做,但如果你这样做,为什么你要把所有的市场对象都放在一个表格上?您可能无法使用th:field
在这种情况下,您必须使用th:name
和th:value
手动创建表单。