java - thymeleaf :field pre-processing is not working with th:each

标签 java spring spring-boot thymeleaf

我想在表格中显示对象列表以及实时更新字段 marketallcoation 之一的选项。我用 thymeleaf 来达到这个目的。因此,我必须将 th:eachth: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。我没有使用过它,所以我不确定我在这里错过了什么。

最佳答案

  1. 如果您使用*{...}语法和/或 th:field属性,那么你必须使用 th:object (它们都依赖 th:object 来发挥作用)。
  2. 您不应该使用 th:field<span/>上元素——这没有意义。 ( 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>

关于java - thymeleaf :field pre-processing is not working with th:each,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60724440/

相关文章:

java - 我的荷兰国旗算法出了什么问题?

java - 无法 Autowiring 。找不到 `Repository' 类型的 bean

java - 基于子实体属性的 JPA 查询

java - 将 Mat 转换为 EM 的一维 float 组?

java - jUnit 测试在 Eclipse 中工作,但当我通过 ant 运行它们时失败

spring - 如何在 JSP 中有条件地检查来自 Spring 的空验证错误消息?

java - 如何将属性文件中的值注入(inject)到字段中?

java - 只是 'DELETE' 请求方法不支持

java - Thymeleaf 3 迭代 List<List<T>> 的语法

Spring Boot 测试 MockMvc 执行后 - 不工作