Search code examples
javaspringthymeleaf

can't reference iteration variable in Thymeleaf


I try to iterate a list of items using th:each,

<div class="row" th:each="item : *{items}">
  <input type="text" th:field="item.name"/>
</div>

it works if I access the iteration variable using th:text, but throws

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'item' available as request attribute

when I use th:field to access it, where did I do wrong?


Solution

  • Something like this could work:

    <div class="row" th:each="item, stat : *{items}">
      <input type="text" th:field="*{items[__${stat.index}__].name}"/>
    </div>
    

    Take a peek here for more info: http://forum.thymeleaf.org/I-have-problem-in-binding-the-list-of-objects-contained-inside-a-object-on-the-form-using-thymeleaf-td3525038.html