Spring Thymeleaf th:每两个迭代两个列表
如何使用thymeleaf th:each同时迭代两个列表Spring Thymeleaf th:每两个迭代两个列表,spring,spring-mvc,thymeleaf,Spring,Spring Mvc,Thymeleaf,如何使用thymeleaf th:each同时迭代两个列表 <select id="rooms" th:field="*{room}"> <option th:each="room : ${roomsFromHotel}" th:value="${{room}}" th:text="${room.id}"> room name </opti
<select id="rooms" th:field="*{room}">
<option th:each="room : ${roomsFromHotel}"
th:value="${{room}}"
th:text="${room.id}">
room name
</option>
</select>
房间名称
这是工作,但我想这样做
<select id="rooms" th:field="*{room}">
<option th:each="room : ${roomsFromHotel}, roomType : ${roomTypesList}"
th:value="${{room}}"
th:text="${roomType.name}">
room name
</option>
</select>
房间名称
不幸的是,你不能那样做
我现在可以想到两个选择:
我更喜欢第二种方法,因为您可以保证实际传递到列表和视图层进行处理的内容。要扩展@Aeseir answers第二个选项,我想最好使用room和roomType创建内部类inside controller方法。这样,您就不会堆积项目结构,并且拥有单一用途的对象,这在本例中是可取的