如何在JSP中以几种不同的方式对List进行排序? [英] How can I sort a List several different ways in a JSP?

查看:172
本文介绍了如何在JSP中以几种不同的方式对List进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Player对象列表从控制器传递到JSP,我想在同一页面上以几种不同的方式显示它们:

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:


  • 按名称排序的菜单

  • 按赢额/损失百分比排序的列表

我可以在模型中放置单独的排序副本,但处理显示相同列表的不同方式似乎更像是视图的责任,所以我想避免将逻辑放在控制器中,如果我能。我已经有几个实现Comparator的类来帮助实际排序。

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.

JSP中最好的方法是什么?

What's the best way to do that in a JSP?

我可以在将列表传递给不同的 forEach 标签之前对其进行排序吗?

Can I sort the list before passing it in to the different forEach tags?

推荐答案

SO EL Tag Wiki 描述了一种在不使用scriptlet的情况下执行此操作的方法:使用EL函数进行排序,然后使用JSTL核心 forEach 标记中 items 属性的结果。

The SO EL Tag Wiki describes 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.

函数类:

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

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>

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

这篇关于如何在JSP中以几种不同的方式对List进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆