Nested loop in Thymeleaf

半城伤御伤魂 提交于 2019-12-11 12:49:06

问题


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!


回答1:


Use th:each:

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


来源:https://stackoverflow.com/questions/52523717/nested-loop-in-thymeleaf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!