代码之家  ›  专栏  ›  技术社区  ›  Rahul Raj

Thymeleaf th:字段预处理不与th:each一起工作

  •  0
  • Rahul Raj  · 技术社区  · 5 年前

    marketallcoation 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>
    

    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 如果使用预处理功能。我没用过,所以我不知道我在这里缺了什么。

    0 回复  |  直到 5 年前
        1
  •  1
  •   Metroids    5 年前
    1. *{...} 语法和/或 th:field 属性,则必须使用 th:object (他们都依赖于 th:对象
    2. 你不应该用 th:字段 <span/> th:字段 设置 name id ,和 value 元素的属性。 名称 价值 不要影响 <span />

    另外,不幸的是我不认为你可以使用 List marketList 作为它的属性之一,然后将其添加到模型中。使新对象 那么预处理应该对你有用。

    <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>