动态字段 thymeleaf 列表迭代
Dynamic fields thymeleaf list iteration
我遇到了一个非常奇怪的错误!在列表上迭代时,thymeleaf 将索引标识为我的 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
returns 当您在 th: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>
实际上,当将字段绑定到表单时,为了访问 doc 指定的 th:each 列表,我们应该使用两个变量 item
和 phoneStat
:
<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>
我遇到了一个非常奇怪的错误!在列表上迭代时,thymeleaf 将索引标识为我的 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
returns 当您在 th: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>
实际上,当将字段绑定到表单时,为了访问 doc 指定的 th:each 列表,我们应该使用两个变量 item
和 phoneStat
:
<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>