我正在将项目的视图层从jsp移动到Thymeleaf,我在这个循环中挣扎。
要创建有效的html输出,我需要嵌套一些<c:if>
- 标签,这些标签会使html在源级别看起来无效。
此外,我还使用了跟踪上一次迭代实例的变量lastStorey
。
我怎样才能在Thymeleaf的th:if
条件下重新创建相同的逻辑?
以下是我要说的代码:
<div class="row">
<div class="col-md-6">
<div class="structure">
<c:set var="lastStorey" value="" />
<c:forEach items="${storeys}" var="c" varStatus="status">
<c:if test="${lastStorey != c[0]}">
<c:if test="${not status.first}">
<span style="clear: left;"></span>
</div>
</c:if>
<div class="storey">
</c:if>
<a href="#"><button><i class="fa fa-music"></i></button></a>
<c:set var="lastStorey" value="${c[0]}" />
<c:if test="${status.last}"><span style="clear:left;"></span></c:if>
</c:forEach>
</div>
</div>
</div>
修改
代码示例的复杂性来自于需要对${storeys}
列表中的属性进行更改。它保存具有storey
- 属性的房间对象。
我现在以${storeys}
是地图的方式更改了代码,每个条目都有一个房间列表。
所以我可以简单地对地图进行交互,并嵌套在里面,迭代房间。
这导致了html代码的大大简化。
感谢您的评论。