欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-07-09 09:34:06
...
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 JSP