我需要实现简单的分页,每页显示100条记录,分页数据按日期排序。虽然我已经正确排序了信息,但是我无法考虑分页逻辑,因为结果集在地图内部,再次将地图保持在里面它,
下面是我的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条记录。 对此表示赞赏。
更新:添加更多信息,这就是我在创建titlelist和titlemap的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,
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);
}
,列表如下所示,
总行数是:160 列表是:[{2012-07-12 = {160 = tstng,159 = testing},2012-07-10 = {158 = test,157 = test,156 = Testing,155 = Testing,154 = Testing,153 =测试,152 =测试,151 =测试,150 =测试,149 =测试,148 =测试,147 =测试,146 =测试,145 =测试,144 =测试,143 =测试,142 =测试,141 =品尝, 140 = Teasting,139 =测试,138 =测试,137 =测试,136 =测试测试,135 =测试测试,134 =测试测试,133 =测试},2012-07-04 = {132 =测试Broker},2012-07-03 = {131 =测试发布,130 = 1200 TESTIN},2012-07-01 = {129 = testing},2012-03-30 = {128 =上传测试},2012-03 -28 = {127 = test},2012-01-08 = {126 = 1BHK持续1000美元在Journal Square},2012-01-04 = {125 = tseting by kiran,124 = tseting by kiran,123 = tseting by kiran,122 = kiran,121 = kiran,120 = kiran,119 = kiran,118 = kiran,117 = kiran,116 = kiran,115 = titing by kiran ,114 = kiran,113 = kiran,112 = kiran,111 = kiran,110 = tseting by kiran,110 = kiran,109 = kiran,108 = kiran,107 = kiran,106 = kiran,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 =测试,77 =测试,76 =测试,75 =测试,74 =测试},2011-12-09 = {73 =每月消费者价格指数较去年同期上涨4.2%,反映出10月份的大幅放松增加了5.5%。 },2011-12-08 = {72 =测试dispplay},2011-11-25 = {71 = Journal Square的公寓出租,价格为1000美元。请看现在。,70 =公寓出租1000美元。请发布。现在看。,69 =测试,68 = AS,67 = A,66 = A,65 = q,64 = q,63 =测试,62 =测试},2011-11-24 = {61 =测试,60 = tEST,59 = TESTING,58 = TESTING,57 =测试,56 =测试,55 =测试,54 = tseting,53 =测试,52 =测试,51 =测试,50 = teting,49 =测试,48 =测试, 47 =测试,46 =测试,45 =测试,44 =测试,43 =测试},2011-11-23 = {42 =测试,41 =测试,40 =测试,39 =测试,38 = TETING,37 =测试},2011-11-22 = {36 =测试,35 =测试,34 =测试,33 = Kiran测试,32 =测试,31 =测试,30 = Kiran测试,29 =测试by kiran,28 = Kiran测试,27 = kiran测试,26 = kiran测试,25 =测试,24 = TSETING,23 = Kiran测试},2011-11-21 = {22 =测试,21 =测试,20 =测试, 19 =测试,18 = ESTING,17 = ESTING,16 = ESTING,15 = ESTING,14 = kiran测试,13 = Kiran测试,12 = Kiran测试,11 = Kiran测试,10 = Testin g by Kian,9 = Kiran测试,8 = Kiran测试,7 = Kran测试,6 =测试,5 = Kiran测试,4 = Kiran测试},2011-11-20 = {3 =测试,2 =测试,1 =测试}}]
答案 0 :(得分:0)
在这种情况下,我假设您正在从数据库中检索所有记录,现在您要对其进行排序并在页面上一次只显示100条记录。要解决此问题,您可以遵循方法
如果您有一个很大的列表,那么以这种方式进行分页可能会影响性能,因为这个列表需要存储在会话中以便来自同一用户的后续请求。
答案 1 :(得分: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>