jstl forEach逆序输出list
2014-12-01 16:09
232 查看
最近在写代码,有一个功能,需要把后台传递到前台的数据逆序输出,本想通过Collections.reverse(list);对list中的数据进行逆序排列后再传递到后台,但想到for循环都可以逆序输出,就想JSTL是否也有可
以进行相关操作,结果搞了半个多钟才写出了下面一段代码
<%@page import="com.jasper.domain.Person"%> <%@page import="java.util.List"%> <%@page import="java.util.ArrayList"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <% //Person对象中有两个属性,age和name List<Person> persons = new ArrayList<Person>(); Person person1 = new Person(); person1.setAge(12); person1.setName("小明"); persons.add(person1); Person person2 = new Person(); person2.setAge(13); person2.setName("阿飞"); persons.add(person2); request.setAttribute("persons", persons); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSTL逆序循环</title> </head> <body> <div>正序</div> <div> <table> <thead> <tr> <th>年龄</th> <th>姓名</th> </tr> </thead> <tbody> <c:forEach var="person" items="${persons }"> <tr> <td>${person.age }</td> <td>${person.name }</td> </tr> </c:forEach> </tbody> </table> </div> <br /> <div>逆序</div> <div> <table> <thead> <tr> <th>年龄</th> <th>姓名</th> </tr> </thead> <tbody> <c:set var="startIndex" value="${fn:length(persons)-1 }"></c:set> <c:forEach var="person" items="${persons }" varStatus="status"> <tr> <td>${persons[startIndex - status.index].age }</td> <td>${persons[startIndex - status.index].name }</td> </tr> </c:forEach> </tbody> </table> </div> <br /> </body> </html>
以进行相关操作,结果搞了半个多钟才写出了下面一段代码
相关文章推荐
- JSTL 的forEach标签循环迭代集合并逆序输出
- Spring MVC使用jstl 标签c:forEach 遍历输出双层嵌套List的数据
- PHP数组FOREACH逆序输出,PHP数组倒序
- jstl 循环 List 输出序号
- jstl+el表达式实现list在jsp页面的输出
- PHP数组foreach逆序输出,php数组倒序
- c++ vector、list 逆序输出
- 关于jstl中foreach遍历list中map的问题
- 编写一个循环将list容器的元素逆序输出
- C#SortedList排序列表怎么样逆序输出
- JSTL 逆序输出
- jsp中用JSTL标签如何判断非空后输出List元素
- list列表循环输出记录 <JSTL>标签
- jstl使用foreach 循环遍历输出表格和数据
- C++ Primer Chapter 9 循环将list元素逆序输出
- jstl forEach 避免list双层循环
- <volist>和<foreach>循环输出list数据
- JSTL-1.2基础 c:forEach 遍历输出字符串数组
- 【数据结构_链表_List_0957】逆序输出链表
- Jstl的foreach循环中用el表达式输出集合