Java 弹簧&x2B;Thymeleaf显示列表中的最后两个元素
我正在使用MVC,并从控制器发送一个元素列表 我怎么能只写列表中的最后2个元素 像这样,我打印列表中的所有元素Java 弹簧&x2B;Thymeleaf显示列表中的最后两个元素,java,spring,spring-mvc,thymeleaf,Java,Spring,Spring Mvc,Thymeleaf,我正在使用MVC,并从控制器发送一个元素列表 我怎么能只写列表中的最后2个元素 像这样,我打印列表中的所有元素 <h2>NEWS</h2> <ul> <li th:each="newsObject : ${news}"> <small class="date"> <div th:text="${newsObject.getDate()}"/></small> <p th:text="${
<h2>NEWS</h2>
<ul>
<li th:each="newsObject : ${news}">
<small class="date"> <div th:text="${newsObject.getDate()}"/></small>
<p th:text="${newsObject.getMessage()}"/>
</li>
</ul>
我应该在.html
文件中使用Thymeleaf做什么来获得“最热门”的新闻?
如下面的示例所示:
2015-11-27 The hotest
2015-11-26 Oldest
我只需要2个元素。有可能吗?您可以将与th:if一起使用
在th:each
中定义迭代状态变量后,可以访问索引
和大小
,这将为您提供列表的当前位置和总长度。th:if
可用于仅包括最后2个元素:
<li th:each="newsObject, iterStat : ${news}" th:if="${iterStat.index >= iterStat.size-2}">
... contents go here ...
</li>
... 内容放在这里。。。
<li th:each="newsObject, iterStat : ${news}" th:if="${iterStat.index >= iterStat.size-2}">
... contents go here ...
</li>