is it possibile to bind a list of list object

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

is it possibile to bind a list of list object

Alexela
Hi,

I have a list of persons and every person has a list of roles.
Can I bind the list of roles using th:field?

Person class has attributes String namePerson, List <Role> roleList
Role class has attribute String roleName

Thanks



 
Reply | Threaded
Open this post in threaded view
|

Re: is it possibile to bind a list of list object

Metroids
Yeah, you can bind to lists of lists of lists of lists objects.

The only requirement I know of is that your th:object has to be an object, and not a list.  So, your command object should be an object with a list of person (and then each person can have a list of roles).  Then you just index into the arrays as normal.

The html would look something like this:

<form th:object="${page}">
    <div th:each="person,i: ${page.persons}">
        <div>
            Name:
            <input type="text" th:field="*{persons[__${i.index}__].namePerson" />
        </div>
        
        <div th:each="role,j: ${person.roleList}">
            Role:
            <input type="text" th:field="*{persons[__${i.index}__].roleList[__${j.index}__].roleName" />
        </div>
    </div>
</form>