簡體   English   中英

動態字段 thymeleaf 列表迭代

[英]Dynamic fields thymeleaf list iteration

我收到了一個非常奇怪的錯誤! 在對列表進行迭代時,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?

實際上,當將字段綁定到表單時,為了訪問 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>

簡單的

當您在th:each之前定義了 1 個變量時, th:each將返回集合中的對象。 不是對象元數據。 如果需要索引,則必須使用 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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM