将列表列表绑定到thymleaf

时间:2017-11-13 04:47:26

标签: java spring-mvc thymeleaf

我可以将List绑定为objectthymleaf的属性,但此列表的属性又为List。这给了我解析错误:

绑定列表:

<form th:object="${obj}" th:action="@{/list}" action="void(0)" method="post">
    <tr>    
        <td th:field="*{id}" th:text="${obj.id}" /> 
    </tr>
    <tr th:each="l , i : ${obj.someList}">
        <input type="hidden" th:field="*{someList[__${i.index}__].something}" />
    </tr>
    <input type="submit" class="btn btn-success" />
</form>

列表再次有一个列表:

public class Foo {
  private int id;
  private List<Some> someList;

  //setter getter

}

public class Some {
  private int sid;
  private List<SomeChild> anotherList;
}

我尝试了以下方法:

<form th:object="${obj}" th:action="@{/list}" action="void(0)" method="post">
    <tr>    
        <td th:field="*{id}" th:text="${obj.id}" /> 
    </tr>
    <tr th:each="l , i : ${obj.someList}">
        <input type="hidden" th:field="*{someList[__${i.index}__].sid}" />
        <!-- till above line it works -->
        <th:block th:each="m, j : ${obj.someList.get(__${i.index}__).anotherList}">
           <!-- till above line no error -->
           <input type="hidden" th:field="*{someList[__${i.index}__].getAnotherList().get(__${j.index}__).id}" />" />
           <!-- error on above line / no readable property -->
        </th:block>
    </tr>
    <input type="submit" class="btn btn-success" />
</form>

我尝试了所有可能的组合,通过索引/ get()和field / getters访问列表,但没有运气。请帮忙。

0 个答案:

没有答案