Quiero mostrar una lista de objetos en una mesa junto con la opción de actualizar uno de los campos marketallcoation
en vivo. Y uso thymeleaf para el propósito. Por lo tanto, tuve que usar th:each
en combinación con la capacidad de pre-procesamiento in disponible th:field
.
En mi clase del controlador, que establece el atributo como se muestra a continuación:
model.addAttribute("marketList",supplyAllocationService.getItems());
Y en mi página HTML, hago algo como esto:
<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>
Cuando ejecuto el código, me sale el siguiente error:
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]
Según los documentos , sin necesidad de th:object
se utiliza si la funcionalidad de pre-procesamiento. No he utilizado de modo. No estoy seguro de lo que me estoy perdiendo aquí. Altamente apreciar si alguien me puede sugerir en este. He intentado un montón de puestos existentes aquí en stackoverflow, pero sin suerte hasta ahora.
- Si está utilizando
*{...}
la sintaxis y / o elth:field
atributo, entonces usted tiene que utilizar unth:object
(ambos dependen delth:object
a función). - No se debe utilizar
th:field
en un<span/>
elemento - que no tiene sentido. (th:field
Juegos de laname
,id
, yvalue
atributos de un elemento.name
Yvalue
no afectan<span />
s.)
También, por desgracia, no creo que se puede utilizar una List
como un objeto de formulario. Así que para fijar su forma, es necesario crear primero un nuevo objeto que tiene marketList
como una de sus propiedades, a continuación, añadir que el modelo en su lugar. Hacer que el nuevo objeto th:object
de su formulario, entonces el procesamiento previo debe trabajar para usted.
<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>