【问题标题】:Dynamic fields thymeleaf list iteration动态字段 thymeleaf 列表迭代
【发布时间】:2015-06-15 13:55:59
【问题描述】:

我遇到了一个非常奇怪的错误!在对列表进行迭代时,thymeleaf 将 index 识别为我的 bean 的属性,而不是索引值!

<div th:each="phoneStat : *{phones}">
    <select th:field="*{phones[__${phoneStat.index}__].variety}">
        <option></option>
    </select>
    <div class=" input-field col s4">
        <input class="validate" th:field="*{phones[__${phoneStat.index}__].number}"
               th:id="${'phonenumber-'+ phones[__${phoneStat.index}__]}" type="text"/>
        <label th:for="${'phonenumber-'+ phones[__${phoneStat.index}__]}"> Mobile</label>
    </div>
</div>

我在这里做错了什么?请帮忙!

2015-06-15 15:48:25.453 ERROR 7764 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "phoneStat.index" (/custom:89)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 10): Property or field 'index' cannot be found on object of type 'com.ubleam.corporate.server.model.Phone' - maybe not public?

【问题讨论】:

    标签: java spring spring-boot spring-mvc thymeleaf


    【解决方案1】:

    实际上,在将字段绑定到表单时,为了访问 doc 指定的带有 th:each 的列表,我们应该使用两个变量 itemphoneStat

    <div th:each="item, phoneStat : *{phones}">
        <select th:field="*{phones[__${phoneStat.index}__].variety}">
            <option></option>
        </select>
        <div class=" input-field col s4">
            <input class="validate" th:field="*{phones[__${phoneStat.index}__].number}"
                   th:id="${'phonenumber-'+ phones[__${phoneStat.index}__]}" type="text"/>
            <label th:for="${'phonenumber-'+ phones[__${phoneStat.index}__]}"> Mobile</label>
        </div>
    </div>
    

    【讨论】:

      【解决方案2】:

      简单

      th:eachth:each 之前定义了 1 个变量时返回集合中的对象。不是对象元数据。如果需要索引,则必须使用 2 个变量。

      http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#using-theach


      详细说明

      为什么要用{phones[__${phoneStat.index}__].number},而phoneStat其实是迭代的对象。

      您可以简单地按照以下方式进行操作。

      <div th:each="phone : *{phones}">
          <select th:field="${phone.variety}" >
              <option></option>
          </select>
          <div class="input-field col s4" >
              <label>Mobile</label>
              <input th:field="${phone.number}" type="text" class="validate"/> 
          </div>
      </div>
      

      【讨论】:

      • 实际上 th:each 需要两个变量来访问索引: th:each="item, rowStat : *{phones}" 当你只使用一个变量时,它作为一个项目而不是作为行,冒号对象(数组),所以索引确实存在@Faraj-farouk thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html 谢谢你的回应:)
      • 但是你的代码只需要 1 个值&lt;div th:each="phoneStat : *{phones}"&gt;
      • 你是对的,这是我犯的错误,我使用 th:each 和一个单行变量,而我应该告诉你
      猜你喜欢
      • 2015-12-12
      • 2017-02-07
      • 2019-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多