Java JSTL forEach逆序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/712046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:37:57  来源:igfitidea点击:

JSTL forEach reverse order

javajspjstl

提问by Steve Kuo

Using JSTL's forEachtag, is it possible to iterate in reverse order?

使用JSTL的forEach标签,是否可以逆序迭代?

采纳答案by Eddie

When you are using forEachto create an integer loop, you can go forward or backward, but it requires some work. It turns out you cannot do this, for example:

当您使用forEach创建整数循环时,您可以前进或后退,但这需要一些工作。事实证明你不能这样做,例如:

<c:forEach var="i" begin="10" end="0" step="-1">
    ....
</c:forEach>

because the spec requires the step is positive. But you can always loop in forward order and then use <c:varto convert the incrementing number into a decrementing number:

因为规范要求该步骤为正值。但是您始终可以按正向顺序循环,然后使用<c:var将递增数字转换为递减数字:

<c:forEach var="i" begin="0" end="10" step="1">
   <c:var var="decr" value="${10-i}"/>
    ....
</c:forEach>

However, when you are doing a forEachover a Collection of any sort, I am not aware of any way to have the objects in reverse order. At least, not without first sorting the elements into reverse order and thenusing forEach.

但是,当您forEach对任何类型的 Collection 进行处理时,我不知道有什么方法可以使对象以相反的顺序排列。至少,首先将元素按相反顺序排序,然后使用forEach.

I have successfully navigated a forEachloop in a desired order by doing something like the following in a JSP:

forEach通过在 JSP 中执行以下操作,我已成功按所需顺序导航循环:

<%
List list = (List)session.getAttribute("list");
Comparator comp = ....
Collections.sort(list, comp);
%>


<c:forEach var="bean" items="<%=list%>">
     ...
</c:forEach>

With a suitable Comparator, you can loop over the items in any desired order. This works. But I am not aware of a way to say, very simply, iterate in reverse order the collection provided.

使用合适的比较器,您可以按任何所需顺序循环遍历项目。这有效。但是我不知道有一种方法可以非常简单地以相反的顺序迭代提供的集合。

回答by harto

You could also consider rolling a custom JSTL function that returned a reversed copy of your list, backed by something like this:

您还可以考虑滚动自定义 JSTL 函数,该函数返回列表的反向副本,由以下内容支持:

public static <T extends Object> List<T> reverse(List<T> list) {
    List<T> copy = Collections.emptyList();
    Collections.copy(copy, list);
    Collections.reverse(copy);
    return copy;
}

Doesn't work for Collections, but as mentioned in another answer, the concept of ordering is a bit vague for some collections.

不适用于集合,但正如在另一个答案中提到的,排序的概念对于某些集合来说有点模糊。

回答by Sunil Mahla

We can do this with a little bit trick:-

我们可以通过一个小技巧来做到这一点:-

<c:forEach begin="1" end="10" var="i" step="1">
     <c:set var="j" value="${10-i+1}" scope="page"></c:set>
     <c:out value="${j}"/>
</c:forEach>

OutPut will be :- 10 9 8 7 6 5 4 3 2 1

输出将是:- 10 9 8 7 6 5 4 3 2 1

回答by yglodt

You could reverse the Collectionbefore adding it to your model.

您可以在将集合添加到模型之前反转集合

This also means you would not need to do anything in the view layer to achieve your requirement.

这也意味着您无需在视图层中执行任何操作即可满足您的要求。

回答by Ian

Building on the answer given by Eddie, I used the following code to iterate over a collection in reverse order.

基于Eddie给出的答案,我使用以下代码以相反的顺序迭代集合。

Given a collection called "list", which stores a list of people.

给定一个名为“list”的集合,它存储一个人的列表。

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>

<%-- Keep a reference to the size of the collection --%>
<c:set var="num_people" value="${fn:length(list)}" />

<%-- Iterate through items. Start at 1 to avoid array index out of bounds --%>
<c:forEach var="i" begin="1" end="${num_people}" step="1">
    <c:set var="person" value="${list[num_people-i]}" />

    ${person.name}<br />
</c:forEach>