Java 如何在 JSP 中以几种不同的方式对 List 进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6638550/
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
How can I sort a List several different ways in a JSP?
提问by Mike Partridge
I have a list of Player objects getting passed into a JSP from a controller, and I want to display them in a couple of different ways on the same page:
我有一个从控制器传递到 JSP 的 Player 对象列表,我想在同一页面上以几种不同的方式显示它们:
- a menu sorted by name
- a list sorted by win/loss percentage
- 按名称排序的菜单
- 按赢/输百分比排序的列表
I could put separate sorted copies in the model, but dealing with different ways to display the same list seems more like a responsibility of the view, so I'd like to avoid putting the logic in the controller if I can. I already have a couple of classes implementing Comparator to help with the actual sorting.
我可以在模型中放置单独的排序副本,但是处理显示相同列表的不同方式似乎更像是视图的责任,所以如果可以的话,我想避免将逻辑放在控制器中。我已经有几个类实现了 Comparator 来帮助进行实际的排序。
What's the best way to do that in a JSP?
在 JSP 中执行此操作的最佳方法是什么?
Can I sort the list before passing it in to the different forEach
tags?
我可以在将列表传递给不同的forEach
标签之前对其进行排序吗?
采纳答案by Mike Partridge
The SO EL Tag Wikidescribes a way to do this without using a scriptlet: using an EL function to do the sort, then using the result for the items
attribute in the JSTL core forEach
tag.
该SO EL标签维基描述的方式来做到这一点,而无需使用小脚本:使用EL函数来完成排序,然后使用该结果items
在JSTL核心属性forEach
标签。
The function class:
函数类:
package org.hierax.ifdl.tags.player;
public final class PlayerSort {
public static List<Player> sortByRank(List<Player> playerList) {
Collections.sort(playerList, new PlayerSortByRank());
return playerList;
}
public static List<Player> sortByAlias(List<Player> playerList) {
Collections.sort(playerList, new PlayerSortByAlias());
return playerList;
}
}
The TLD:
顶级域名:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<display-name>Player Functions</display-name>
<tlib-version>1.0</tlib-version>
<short-name>player</short-name>
<uri>org.hierax.ifdl.tags</uri>
<function>
<name>sortByRank</name>
<function-class>org.hierax.ifdl.tags.player.PlayerSort</function-class>
<function-signature>java.util.List sortByRank(java.util.List)</function-signature>
</function>
<function>
<name>sortByAlias</name>
<function-class>org.hierax.ifdl.tags.player.PlayerSort</function-class>
<function-signature>java.util.List sortByAlias(java.util.List)</function-signature>
</function>
</taglib>
The menu JSP:
菜单 JSP:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="p" uri="/WEB-INF/player.tld" %>
<h1>Players</h1>
<p>
<c:forEach items="${p:sortByAlias(model.players)}" var="player">
<a href="<c:url value="/player/${player.id}"/>" class="menuItem">${player.alias}</a>
</c:forEach>
</p>
回答by Bohemian
Employ Collections.sort(List, Comparator), providing your own Comparator as required to sort the list into the right order. The java, which could be embedded within <%
and %>
tags, would look something like:
使用Collections.sort(List, Comparator),根据需要提供您自己的 Comparator 以将列表排序为正确的顺序。可以嵌入在<%
和%>
标签中的 java看起来像:
List<Player> list = new ArrayList<Player>();
list.add(new Player()); // populate list
list.add(new Player()); // etc
Collections.sort(list, new Comparator<Player>() {
public int compare(Player o1, Player o2)
{
return o1.getName().compareTo(o2.getName()); // Compare by name, for example
}});
// Now iterations on list will be in 'name' order
for (Player player : list) {
// Display player
}
回答by g051051
I'd recommend having two sorted lists in the model. The lists will just contain object references, so it's not a big space issue, and I personally don't like doing that sort of work in the JSPs. If you presort them, it doesn't matter how many times the page is viewed in normal navigation, and as long as the lists don't change, you won't have to incur the sort overhead.
我建议在模型中有两个排序列表。列表将只包含对象引用,所以这不是一个很大的空间问题,而且我个人不喜欢在 JSP 中做那种工作。如果您对它们进行预排序,则在正常导航中查看页面的次数无关紧要,只要列表不更改,您就不必承担排序开销。