1. 程式人生 > >jstl forEach逆序輸出list

jstl forEach逆序輸出list

最近在寫程式碼,有一個功能,需要把後臺傳遞到前臺的資料逆序輸出,本想通過Collections.reverse(list);對list中的資料進行逆序排列後再傳遞到後臺,但想到for迴圈都可以逆序輸出,就想JSTL是否也有可
  1.     <%@pageimport="com.jasper.domain.Person"%>  
  2.     <%@pageimport="java.util.List"%>  
  3.     <%@pageimport="java.util.ArrayList"%>  
  4.     <%@ page language="java" contentType=
    "text/html; charset=UTF-8"
  5.         pageEncoding="UTF-8"%>  
  6.     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
  7.     <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>  
  8.     <%  
  9.         //Person物件中有兩個屬性,age和name
  10.         List<Person> persons = new ArrayList<Person>();  
  11.         Person person1 = new Person();  
  12.         person1.setAge(12);  
  13.         person1.setName("小明");  
  14.         persons.add(person1);  
  15.         Person person2 = new Person();  
  16.         person2.setAge(13);  
  17.         person2.setName("阿飛");  
  18.         persons.add(person2);  
  19.         request.setAttribute("persons"
    , persons);  
  20.     %>  
  21. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">  
  22.     <html>  
  23.     <head>  
  24.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  25.     <title>JSTL逆序迴圈</title>  
  26.     </head>  
  27.     <body>  
  28.         <div>正序</div>  
  29.     <div>  
  30.             <table>  
  31.                 <thead>  
  32.                     <tr>  
  33.                         <th>年齡</th>  
  34.                         <th>姓名</th>  
  35.                     </tr>  
  36.                 </thead>  
  37.                 <tbody>  
  38.                     <c:forEach var="person" items="${persons }">  
  39.                         <tr>  
  40.                             <td>${person.age }</td>  
  41.                             <td>${person.name }</td>  
  42.                     </tr>  
  43.                     </c:forEach>  
  44.                 </tbody>  
  45.             </table>  
  46.         </div>  
  47.         <br />  
  48.     <div>逆序</div>  
  49.         <div>  
  50.             <table>  
  51.                 <thead>  
  52.                     <tr>  
  53.                         <th>年齡</th>  
  54.                         <th>姓名</th>  
  55.                     </tr>  
  56.                 </thead>  
  57.                 <tbody>  
  58.                     <c:set var="startIndex" value="${fn:length(persons)-1 }"></c:set>  
  59.                     <c:forEach var="person" items="${persons }" varStatus="status">  
  60.                         <tr>  
  61.                             <td>${persons[startIndex - status.index].age }</td>  
  62.                             <td>${persons[startIndex - status.index].name }</td>  
  63.                         </tr>  
  64.                     </c:forEach>  
  65.                 </tbody>  
  66.             </table>  
  67.         </div>  
  68.         <br />  
  69.     </body>  
  70.     </html>  

以進行相關操作,結果搞了半個多鍾才寫出了下面一段程式碼