0

我需要实现简单的分页,每页显示 100 条记录,并且分页数据按日期排序。虽然我已经对信息进行了正确排序,但是我无法考虑分页逻辑,因为结果集在 map 内部,它再次在其中保存 Map,

下面是我的 JSTL 代码以及 TitleList/Title 的定义,我在其中添加结果集数据后正在对其进行迭代。

 Map<Date, LinkedHashMap<Integer, String>> TitleList = new LinkedHashMap<Date, LinkedHashMap<Integer, String>>();

LinkedHashMap<Integer, String> Title = new LinkedHashMap<Integer, String>();

<c:forEach items="${TitleList}" var="column"> 
            <p><c:out value="${column.key}"/></p>                      
            <c:forEach var="Title" items="${column.value}">
                <p><a href="<%=request.getContextPath()%>/Test.do?id=${Title.key}"><c:out value="${Title.value}"/></a></p>
            </c:forEach>
        </c:forEach> 

虽然我可以通过将结果集直接添加到列表来对其进行分页,但是这会被我正在寻找的排序顺序和功能杀死。不知何故,对于上面的代码,我需要限制每页 100 条记录,然后实现 Next 按钮,它将再次显示 100记录。感谢对此的一些想法。

更新:添加更多信息,这是我创建标题列表和标题图的 servlet 中的地图外观,

map = {2012-07-12={160=tstng, 159=testing}, 2012-07-10={158=test, 157=test, 156=Testing, 155=Testing, 154=Testing, 153=Testing, 152=Testing, 151=Testing, 150=Testing, 149=Testing, 148=Testing, 147=Testing, 146=Testing, 145=Testing, 144=Testing, 143=Testing, 142=Testing, 141=Teasting, 140=Teasting, 139=Testing, 138=Testing, 137=Testing , 136=Testing for testing, 135=Testing for testing, 134=Testing for Test, 133=Testing}, 2012-07-04={132=Testing for Broker}, 2012-07-03={131=Testing for Post, 130=1200 TESTIN}, 2012-07-01={129=testing}, 2012-03-30={128=upload test}, 2012-03-28={127=test}, 2012-01-08={126=1BHK flat for 1000$ at Journal Square}, 2012-01-04={125=tseting by kiran, 124=tseting by kiran, 123=tseting by kiran, 122=tseting by kiran, 121=tseting by kiran, 120=tseting by kiran, 119=tseting by kiran, 118=tseting by kiran, 117=tseting by kiran, 116=tseting by kiran, 115=tseting by kiran, 114=tseting by kiran, 113=tseting by kiran, 112=tseting by kiran, 111=tseting by kiran, 110=tseting by kiran, 109=tseting by kiran, 108=tseting by kiran, 107=tseting by kiran, 106=tseting by kiran, 105=tseting by kiran, 104=tseting by kiran}, 2012-01-02={103=testing, 102=testing, 101=testing, 100=testing, 99=testing, 98=testing, 97=testing, 96=testing, 95=testing, 94=testing, 93=testing, 92=testing, 91=testing, 90=testing, 89=testing, 88=testing, 87=testing, 86=testing, 85=testing, 84=testing, 83=testing, 82=testing, 81=testing, 80=testing, 79=testing, 78=testing, 77=testing, 76=testing, 75=testing, 74=testing}, 2011-12-09={73=The monthly consumer price index rose 4.2% from the year-ago period, reflecting a huge easing from Octoberâ??s 5.5% increase. }, 2011-12-08={72=Testing for dispplay}, 2011-11-25={71=Apartment for Rent at Journal Square for 1000$.Please see now., 70=Apartment for Rent at Journal Square for 1000$.Please see now., 69=testing, 68=AS, 67=A, 66=A, 65=q, 64=q, 63=Testing, 62=Testing}, 2011-11-24={61=testing, 60=tEST, 59=TESTING, 58=TESTING, 57=Testing, 56=Testing, 55=Testing, 54=tseting, 53=testing, 52=testing, 51=testing, 50=teting, 49=tESTING, 48=tESTING, 47=tESTING, 46=Testing, 45=TESTING, 44=TESTING, 43=tESTING}, 2011-11-23={42=Testing, 41=testing, 40=Testing, 39=tESTING, 38=TETING, 37=testing}, 2011-11-22={36=Testing, 35=Testing, 34=Testing, 33=Testing by Kiran, 32=testing, 31=Testing, 30=Testing by Kiran, 29=Testing byt kiran, 28=Testing by Kiran, 27=testing by kiran, 26=testing by kiran, 25=Testing , 24=TSETING, 23=Testing by Kiran}, 2011-11-21={22=testing, 21=Testng, 20=TESTING, 19=TESTING, 18=ESTING, 17=ESTING, 16=ESTING, 15=ESTING, 14=testing by kiran, 13=testing by Kiran, 12=Testing by Kiran, 11=testing by Kiran, 10=Testing by Kian, 9=Testing by Kiran, 8=Testing by Kiran, 7=Testing byKiran, 6=testing, 5=Test by Kiran, 4=Test by Kiran}, 2011-11-20={3=testing, 2=testing, 1=testing}}

下面是 servlet 代码 I,它将在从 DB 读取后创建映射,

 Map<Date, LinkedHashMap<Integer, String>> TitleList = new LinkedHashMap<Date, LinkedHashMap<Integer, String>>();
            String sqld = "Select Distinct DATE(timestamp) as DATE from table ORDER BY DATE DESC LIMIT 730 ;";
            ResultSet rsd = null;
            try {
                rsd = stmt1.executeQuery(sqld);
            } catch (SQLException ex) {
                Logger.getLogger(myclass.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
                while (rsd.next()) {
                    LinkedHashMap<Integer, String> Title = new LinkedHashMap<Integer, String>();
            //        System.out.println("Testing");
                    Date Date = rsd.getDate("Date");
            //        System.out.println(Date);
                    String sql = "Select title,id from table Where DATE(timestamp) = '" + Date + "'ORDER BY id DESC";
           //         System.out.println(sql);
                    ResultSet rs = stmt.executeQuery(sql);
                    rs.last();
                    int numRows = rs.getRow();
          //          System.out.println(rs.getRow());
                    rs.beforeFirst();
                    while (rs.next()) {                                  
                        for (int i = 1; i <= numRows; i++) {
                            String title = rs.getString("title");
                            Integer id = rs.getInt("id");
                               Title.put(id, title);                         
                            TitleList.put(Date, Title);
                        }
                        System.out.println("Size of LinkedHashMap for Title  : " + Title.size());
         //               System.out.println(rs.getString("title"));
                    }

                }

这是我尝试过 Richz 的东西,这是我在 dao 课上得到的东西,我做了类似下面的事情,

string query ="select title,id,date(timestamp) as date from table";
list<TitleList> list = new ArrayList<Titlelist>(); 
try {

 resultset rs = stmt.executeQuery(query);
    while(rs.next()){
     myclass myc = new myclass();
     myc.setcid(rs.getInt("id");
     myc.settitle(rs.getString("title");
     myc.settimestamp(rs.gettimestamp("timestamp");
     list.add(myc);
    }

并在jsp中

<c:foreach var="Titlelist" items="${Titlelist}">
  EL for title,id and title
</c:foreach>
With this approach I can do pagination date wise along pages and data looks like below along with pagination

Date10 title10
Date10 title09



However I need data in this format,
Date 10
title10
title09

我尝试做列表列表,但由于某些原因我无法做到。我正在失去列之间的关系。我的互联网搜索显示我的分页,如上面对列表所做的那样。

8 月 31 日更新详情,

Ok Richz 这里是我的 JSP,我使用的结构和你的完全相同。

<%@page import="java.util.LinkedHashMap"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/header.jsp" %>
<%@ include file="WEB-INF/jspf/navdisplay.jspf" %>
<%@page import="java.util.ArrayList"%>
<%@page session="true" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> 

<!DOCTYPE html>
<html>
    <head>
        <title>DB Test</title>
    </head>
    <body>
        <c:set var="dateTotal" scope="session" value="${fn:length(list)}"/>
        <c:set var="currentDate" value="${param.date}"/>
        <c:if test="${empty currentDate or currentDate < 0}">
            <c:set var="currentDate" value="0"/>
        </c:if>
        <c:if test="${dateTotal <= currentDate}">
            <c:set var="currentDate" value="${dateTotal - 1}"/>
        </c:if>
        <c:set var="perPage" scope="session"  value="10"/>
        <c:set var="pageStart" value="${param.start}"/>
        <c:if test="${empty pageStart or pageStart < 0}">
            <c:set var="pageStart" value="0"/>
        </c:if>
        <a href="?date=${currentDate - 1}">Previous Date</a>  <a  href="?date=${currentDate + 1}">Next Date</a><br/>
        Titles <a href="?start=${pageStart - perPage}"><<</a>${pageStart + 1} - ${pageStart + perPage} <a  href="?start=${pageStart + perPage}">>></a>
        <c:forEach items="${list[currentDate]}" var="item" > 
            <p><c:out value="${item.key}"/></p> 
            <c:forEach var="Title" items="${item.value}" begin="${pageStart}" end="${pageStart + perPage - 1}" >
                <p><a href="<%=request.getContextPath()%>/Test.do?id=${Title.key}"><c:out value="${Title.value}"/></a></p>   
            </c:forEach>                  
        </c:forEach>
    </body>
</html>

下面是为列表生成数据的servlet,

列表列表 = 新的 ArrayList();

    Map<Date, LinkedHashMap<Integer, String>> TitleList = new LinkedHashMap<Date, LinkedHashMap<Integer, String>>();
    String sqld = "Select Distinct DATE(ctimestamp) as DATE from table ORDER BY DATE DESC LIMIT 730 ;";
    ResultSet rsd = null;
    try {
        rsd = stmt1.executeQuery(sqld);
    } catch (SQLException ex) {
        Logger.getLogger(sortResultshs.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        while (rsd.next()) {
            LinkedHashMap<Integer, String> Title = new LinkedHashMap<Integer, String>();
    //        System.out.println("Testing");
            Date Date = rsd.getDate("Date");
   //         System.out.println(Date);

            String sql = "Select title,id from table Where DATE(ctimestamp) = '" + Date + "'ORDER BY id DESC";
    //        System.out.println(sql);
            ResultSet rs = stmt.executeQuery(sql);
            rs.last();
            int numRows = rs.getRow();
  //          System.out.println(rs.getRow());
            rs.beforeFirst();
            while (rs.next()) {
                //        List<String> Title = new ArrayList<String>();               
                for (int i = 1; i <= numRows; i++) {
                    String title = rs.getString("title");
                    Integer id = rs.getInt("id");
                    Title.put(id, title);
                    TitleList.put(Date, Title);                         

                }

 //               System.out.println(rs.getString("title"));
            }

             list.add(TitleList);

        }

列表如下所示,

118=kiran 设置,117=kiran 设置,116=kiran 设置,115=kiran 设置,114=kiran 设置,113=kiran 设置,112=kiran 设置,111=kiran 设置,110= kiran tseting, 109=kiran tseting, 108=kiran tseting, 107=kiran tseting, 106=kiran tseting, 105=kiran tseting, 104=kiran tseting}, 2012-01-02={103=测试, 102=测试, 101=测试, 100=测试, 99=测试, 98=测试, 97=测试, 96=测试, 95=测试, 94=测试, 93=测试, 92=测试, 91=测试, 90=测试,89=测试,88=测试,87=测试,86=测试,85=测试,84=测试,83=测试,82=测试,81=测试,80=测试,79=测试,78= testing, 77=testing, 76=testing, 75=testing, 74=testing}, 2011-12-09={73=月度消费者物价指数较去年同期上涨 4.2%,反映出 10 月以来的大幅回落。增加 5.5%。},

4

2 回答 2

0

请尝试此演示页面。

<%@ page import="java.util.*" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
  //dummy data
  List<Map> list = new ArrayList<Map>();
  long now = new Date().getTime();
  for(int i = 1; i < 4; i++){
     LinkedHashMap<Integer, String> titleMap = new LinkedHashMap<Integer, String>();
     for(int j = 1; j < 43 ;j++){
         titleMap.put(j, "title" + j);
     }
     Map<Date, LinkedHashMap<Integer, String>> dateMap = new LinkedHashMap<Date,    LinkedHashMap<Integer, String>>();
     dateMap.put(new Date(now + 1000*60*60*25* i), titleMap);
     list.add(dateMap);
 }
 pageContext.setAttribute("list", list);
%>
<c:set var="dateTotal" scope="session" value="${fn:length(list)}"/>
<c:set var="currentDate" value="${param.date}"/>
<c:if test="${empty currentDate or currentDate < 0}">
    <c:set var="currentDate" value="0"/>
</c:if>
<c:if test="${dateTotal <= currentDate}">
    <c:set var="currentDate" value="${dateTotal - 1}"/>
</c:if>
<c:set var="perPage" scope="session"  value="10"/>
<c:set var="pageStart" value="${param.start}"/>
<c:if test="${empty pageStart or pageStart < 0}">
    <c:set var="pageStart" value="0"/>
</c:if>
<a href="?date=${currentDate - 1}">Previous Date</a>  <a  href="?date=${currentDate + 1}">Next Date</a><br/>
Titles <a href="?start=${pageStart - perPage}"><<</a>${pageStart + 1} - ${pageStart + perPage} <a  href="?start=${pageStart + perPage}">>></a>
<c:forEach items="${list[currentDate]}" var="item" > 
     <p><c:out value="${item.key}"/></p> 
     <c:forEach var="Title" items="${item.value}" begin="${pageStart}" end="${pageStart + perPage - 1}" >
          <p><a href="<%=request.getContextPath()%>/Test.do?id=${Title.key}"><c:out value="${Title.value}"/></a></p>   
     </c:forEach>                  
</c:forEach>
于 2012-08-09T15:12:02.607 回答
0

在这种情况下,我假设您正在检索数据库中的所有记录,现在您想要对其进行排序并在页面上一次仅显示 100 条记录。要解决此问题,您可以按照以下方法

  1. 将结果存储在主数组列表中。然后使用 public List subList(int fromIndex, int toIndex) 提取前100条记录并将这些数据存储到请求范围中并在上面提到的jsp中使用它
  2. 当用户单击 Next 按钮时,使用主数组列表,现在获取下一组 100 条记录,并再次显示带有新记录集的页面。确保您维护一个变量,该变量具有当前记录的 startIndex 计数。

如果您有一个大列表,以这种方式进行分页可能会影响性能,因为该列表将需要存储在会话中以供同一用户的后续请求。

于 2012-08-09T10:09:30.830 回答