'Nested loop in Thymeleaf

I'm a newbie to Thymeleaf.

I have two objects- Classroom & Student: each Classroom contains a List<Student> and I can have a list of classrooms: List<Classroom>.

What I want to be able to do with Thymeleaf is the equivalent of the below java code:

            for(int i = 0; i < classroomList.size(); i++){
                System.out.println(classroomList.get(i).getRoomName());
                for(int x = 0; x < studentList.size(); x++){
                    System.out.println(studentList.get(x));
                }
            }

So the output would be: {classroom1{joe1,joe2}, classroom2{joe3}}...

But I need to be able to do this in HTML with Thymeleaf (by passing a list of classrooms) so I can make it look nice.

Any help is much appreciated. Thanks!



Solution 1:[1]

Use th:each:

<div th:each="classroom : ${classroomList}">
    <div th:text="${classroom.name}"></div> 
    <ul>
      <div th:each="student : ${classroom.studentList}">
         <li>"${student.name}"</li>
      </div>
    </ul>
</div>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1