对内存中Lucene查询的集合进行分页

本文介绍了一种基于Java的分页工具实现方案,包括自定义分页工具类Calculator及页面集成方式pageTool.jsp。该方案支持自定义每页显示记录数、计算总页数和记录数等功能,并提供了灵活的页面链接生成机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、页面,显示分页工具栏

<jsp:include page="pageTool.jsp">
<jsp:param name="counts" value="<%=resultList.size() %>"/>
<jsp:param name="limit" value="<%=limit %>"/>
<jsp:param name="currentPage" value="<%=currentPage %>"/>
</jsp:include>

2、分页工具类


package com.trade.commen;

public class Calculator {

int limit = 10;//默认显示10条
int pagesNum = 0;//总页数
int counts = 0;//总记录数
public Calculator(int limit, int counts) {
super();
this.limit = limit;
this.counts = counts;
this.pagesNum = getPagesNum();
}

/**
* 获取总页数
* @return 总页数
*/
public int getPagesNum(){
if(counts >0 && counts <=limit){
return 1;
}else{
int temp = counts/limit;
if(counts%limit !=0){
return temp+1;
}else{
return temp;
}
}
}

/**
* 获取查询起始位置
* @param currentPage
*/
public int getStart(int currentPage){
if(currentPage <= 1){
return 0;
}else{
return (currentPage-1)*limit;
}
}

/**
* 获取查询结束位置
* @param currentPage
*/
public int getEnd(int currentPage){
System.out.println("###########currentPage:"+currentPage);
System.out.println("###########pagesNum:"+pagesNum);
if(currentPage == pagesNum){
int yushu = counts%limit;
System.out.println("###########yushu:"+yushu);

int end = getStart(currentPage)+yushu;
System.out.println("###########end:"+end);
return end;
}else{
return getStart(currentPage)+limit;
}

}
}


3、pageTool.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.trade.commen.Calculator"%>
<%@page import="java.util.Map.Entry"%>

<%
//功能**********************对内存中的集合进行分页**********************

int start = 0;//起始位置
int end = 1;//结束位置
int limit = 10;//默认显示10条
int pagesNum = 0;//总页数
int currentPage = 1;//当前页
int counts = 0;//总记录数
String viewUrl = "supply_list.jsp";//显示分页结果的页面

//=================获取参数==================
if(request.getParameter("counts") != null && !request.getParameter("counts").equals("0")){
counts = Integer.parseInt(request.getParameter("counts"));
}
if(request.getParameter("start") != null && !request.getParameter("start").equals("0")){
start = Integer.parseInt(request.getParameter("start"));
}
if(request.getParameter("limit") != null && !request.getParameter("limit").equals("0")){
limit = Integer.parseInt(request.getParameter("limit"));
}
if(request.getParameter("currentPage") != null && !request.getParameter("currentPage").equals("0")){
currentPage = Integer.parseInt(request.getParameter("currentPage"));
}
if(request.getParameter("viewUrl") != null && !request.getParameter("viewUrl").equals("0")){
viewUrl = request.getParameter("viewUrl");
}

Map criteria = (Map)request.getAttribute("criteria");
String condition = "?";
if(null != criteria && criteria.size() > 0){
Iterator it = criteria.entrySet().iterator();
while(it.hasNext()){
Entry entry = (Entry)it.next();
String paraName = entry.getKey().toString();
String value = entry.getValue().toString();
condition += paraName+"="+value+"&";
}
}
viewUrl = viewUrl+condition;

//===============显示分页工具条=================
Calculator cal = new Calculator(limit,counts);
pagesNum = cal.getPagesNum();
if(pagesNum >= 1){
//总记录
out.print("Total:"+counts+"  ");
//总页数
out.print("Pages:"+cal.getPagesNum()+"   ");
//首页
out.print("<a href="+viewUrl+"start=0"+"&end="+cal.getEnd(1)+"&currentPage=1"+" >"+"First</a>  ");

//上一页
if(currentPage-1 > 0){
out.print("<a href="+viewUrl+"start="+cal.getStart(currentPage-1)+"&end="+cal.getEnd(currentPage-1)+"&currentPage="+(currentPage-1)+" >"+" Previous </a>  ");
}
if(pagesNum <= 11){//pagesNum<=11
for(int i=1;i<=pagesNum;i++){
if(i == currentPage){
out.print("<b> <a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a> </b>  ");
}else{
out.print("<a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a>  ");
}
}
}else{//总页数大于11
if(currentPage <= 6){
for(int i=1;i<= 11;i++){//前11个
if(i == currentPage){
out.print("<b> <a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a> </b>  ");
}else{
out.print("<a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a>  ");
}
}
}else if(currentPage > 6 && currentPage <= pagesNum-5){//当前页大于6小于尾页-5
for(int i=currentPage-5;i< currentPage;i++){//当前页前5页
if(i == currentPage){
out.print("<b> <a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a> </b>  ");
}else{
out.print("<a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a>  ");
}
}
for(int i=currentPage;i<=currentPage+5;i++){//当前页后5页
if(i == currentPage){
out.print("<b> <a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a> </b>  ");
}else{
out.print("<a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a>  ");
}
}
}else if(currentPage > currentPage-5){
for(int i=cal.getPagesNum()-10;i<=cal.getPagesNum();i++){
if(i == currentPage){
out.print("<b> <a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a> </b>  ");
}else{
out.print("<a href="+viewUrl+"start="+cal.getStart(i)+"&end="+cal.getEnd(i)+"&currentPage="+i+" >"+i+"</a>  ");
}
}
}
}

//下一页
if(currentPage+1 <= cal.getPagesNum()){
out.print("<a href="+viewUrl+"start="+cal.getStart(currentPage+1)+"&end="+cal.getEnd(currentPage+1)+"&currentPage="+(currentPage+1)+" >"+" Next </a>  ");
}

//尾页
out.print("<a href="+viewUrl+"start="+cal.getStart(cal.getPagesNum())+"&end="+cal.getEnd(cal.getPagesNum())+"&currentPage="+cal.getPagesNum()+" >Last"+"</a> ");
}


%>
lucene搜索分页过程中,可以有两种方式 一种是将搜索结果集直接放到session中,但是假如结果集非常大,同时又在大并发访问的时候,很可能造成服务器的内存不足,而使服务器宕机 还有一种是每次都重新进行搜索,这样虽然避免了内存溢出的可能,但是,每次搜索都要进行一次IO操作,如果大并发访问的时候,你要保证你的硬盘的转速足够的快,还要保证你的cpu有足够高的频率 而我们可以将这两种方式结合下,每次查询都多缓一部分的结果集,翻页的时候看看所查询的内容是不是在已经在在缓当中,如果已经在了就直接拿出来,如果不在,就进行查询后,从缓中读出来. 比如:现在我们有一个搜索结果集 一个有100条数据,每页显示10条,就有10页数据. 安装第一种的思路就是,我直接把这100条数据缓起来,每次翻页时从缓种读取 而第二种思路就是,我直接从搜索到的结果集种显示前十条给第一页显示,第二页的时候,我在查询一次,给出10-20条数据给第二页显示,我每次翻页都要重新查询 第三种思路就变成了 我第一页仅需要10条数据,但是我一次读出来50条数据,把这50条数据放入到缓当中,当我需要10--20之间的数据的时候,我的发现我的这些数据已经在我的缓在了,我就直接中把数据读出来,少了一次查询,速度自然也提高了很多. 如果我访问第六页的数据,我就把我的缓更新一次.这样连续翻页10次才进行两次IO操作 同时又保证了内存不容易被溢出.而具体缓设置多少,要看你的服务器的能力和访问的人数来决定
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值