Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
551 views
in Technique[技术] by (71.8m points)

java - How can I sort a List several different ways in a 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:

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

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...