0

I need to Implement simple pagination displaying 100 records per page and pagination data is sorted by date.Though I have sorted the information correctly,however I am unable to think about pagination logic since resultset is inside map which is again holding Map inside it,

Below is my JSTL Code along with definition for TitleList/Title which I am iterating after adding resultset data in it.

 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> 

Though I can paginate it by adding resultset directly to list ,however that will kill by sorting order and functionality I am looking for.Somehow for above code, I need to restrict the 100 records per page and then implement Next button which will again display 100 records. Appreciate some thoughts on this.

Updated:Adding more information,this is how map looks like from servlet where I am creating titlelist and title maps,

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

Below is servlet code I which will create map after reading from 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"));
                    }

                }

here is something I tried Richz, Here is what I am getting in dao class I did something like below,

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

and in 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

I tried doing list of lists but for some reasons I am unable to do it.I am losing relations between columns.My internet search shows me pagination as done above for list.

Updated Details on 31st August,

Ok Richz here is my JSP, I have used exactly the same structure as yours.

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

Below is servlet which is generating data for list,

List list = new 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);

        }

and the list looks something like below,

Total Row count is : 160 List is : [{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 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}}]

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

このシナリオでは、DB からすべてのレコードを取得していて、それを並べ替えてページに一度に 100 レコードだけを表示したいとします。この問題を解決するには、次のアプローチを使用できます

  1. 結果をマスター配列リストに格納します。次に public List subList(int fromIndex, int toIndex) を使用して最初の 100 レコードを抽出し、このデータをリクエスト スコープに格納して、上記のように jsp で使用します。
  2. ユーザーが [次へ] ボタンをクリックすると、マスター配列リストが使用され、次の 100 レコードのセットが取得され、新しいレコード セットでページが再度表示されます。現在のレコードの startIndex のカウントを持つ変数を維持していることを確認してください。

この方法でページネーションを行うと、大きなリストがある場合、パフォーマンスが低下する可能性があります。このリストは、同じユーザーからの後続のリクエストのためにセッションに保存する必要があるためです。

于 2012-08-09T10:09:30.830 に答える