Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧&x2B;Thymeleaf显示列表中的最后两个元素_Java_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 弹簧&x2B;Thymeleaf显示列表中的最后两个元素

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="${

我正在使用MVC,并从控制器发送一个元素列表

我怎么能只写列表中的最后2个元素

像这样,我打印列表中的所有元素

<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>