2017-09-27 240 views
0

我有一个名为'notifications'的列表,我使用Thymeleaf'each method'来逐个访问它的元素。我可以做到这一点,如下所示。Thymeleaf:如何以相反顺序遍历列表?

<li th:each="n : *{notifications}"> 
    <h4 type="text" th:text="*{n.message}"></h4> 
</li> 

注意:'message'是我需要从列表中检索的属性。

如何以相反顺序访问元素?例如,如果这是我的当前输出,

Cat 
Dog 
Rat 

我该如何获得输出?

Rat 
Dog 
Cat 
+2

扭转在服务器端的列表,也许。 – glytching

回答

2

如果可能的话,我会反转它的服务器端。如果你不想这样做,也许这样的事情会为你工作:

<li th:each="i : ${#numbers.sequence(notifications.size() - 1, 0, -1)}"> 
    <h4 type="text" th:text="${notifications[i].message}"></h4> 
</li>