java 分页

import javax.servlet.http.HttpServletRequest;
/**
 * 该类主要是用于分页。
 * @author 邬小洪
 * 2009.01.02
 */
public class PagesUtil {
 //总记录条数
 private int totalRecords=0;
 //总页面数
 private int totalPages=0;
 //当前页数
 private int currentPage=0;
 //是否初始化
 private boolean inited=false;
 /**
  * 初始化参数
  * @param request 页面请求
  * @param allRecord 总记录数
  * @param pageRows 每页显示的记录数
  */
 public void init(HttpServletRequest request,int totalRecordsIn,int pageRows){
  
  totalRecords = totalRecordsIn;
  //获得总页面数
  totalPages = (totalRecords + pageRows - 1) / pageRows;
  //判断参数currentPage是否为空
  String currentPageStr = request.getParameter("currentPage");
  if (currentPageStr == null||currentPageStr.length()==0)
   currentPage = 0;//初始值为0
  else
   currentPage = Integer.parseInt(currentPageStr);
  //判断当前页
  if (currentPage < 0) {
   currentPage = 0;
  } else if (currentPage > totalPages - 1 && totalPages >= 1) {
   currentPage = totalPages - 1;
  }
  inited=true;
 }
 /**
  * 返回分页的结果.
  * @param recordPages 当前页前后显示的页面数
  * @param urlParam 提交页面地址
  * @return 1 2 3 4 5 6 7 8 9 10 11  共332条  共34页  第[6]页  首页  上一页  下一页  尾页
  */
 public String getPages(int recordPages,String urlParam){
  if(!inited){
   System.out.println("请先调用初始化方法:init(HttpServletRequest request,int allRecord,int pageRows)");
   return "";
  }
  StringBuffer pageBuffer = new StringBuffer();
  if(totalRecords!=0){
   for (int n = 0; n < totalPages; n++){
    if(n<=(currentPage+recordPages)&&n>=(currentPage-recordPages)){
     if(currentPage==n){
      pageBuffer.append("<font color='red'>");
      pageBuffer.append(n+1);
      pageBuffer.append("</font>");
     }else{
      pageBuffer.append("<a href='");
      pageBuffer.append(urlParam);
      pageBuffer.append("&currentPage=");
      pageBuffer.append(n);
      pageBuffer.append("'>");
      pageBuffer.append(n+1);
      pageBuffer.append("</a>");
     }
     pageBuffer.append("&nbsp;");
    }
   }
   
   pageBuffer.append("&nbsp;");
   
   pageBuffer.append("共");
   pageBuffer.append(totalRecords);
   pageBuffer.append("条");
   
   pageBuffer.append("&nbsp;&nbsp;");
   
   pageBuffer.append("共");
   pageBuffer.append(totalPages);
   pageBuffer.append("页");
   
   pageBuffer.append("&nbsp;&nbsp;");
   
   pageBuffer.append("第[<font color='red'>");
   pageBuffer.append(currentPage+1);
   pageBuffer.append("</font>]页");
   
   pageBuffer.append("&nbsp;&nbsp;");
   
   if(totalPages>1){
//    首页
    if(totalPages>1&&currentPage>0){
     pageBuffer.append("<a href='");
     pageBuffer.append(urlParam);
     pageBuffer.append("&currentPage=0");
     pageBuffer.append("'>首页</a>");
    }else{
     pageBuffer.append("首页");
    }
    
    pageBuffer.append("&nbsp;&nbsp;");
    
    //上一页
    if(currentPage>0){
     pageBuffer.append("<a href='");
     pageBuffer.append(urlParam);
     pageBuffer.append("&currentPage=");
     pageBuffer.append(currentPage-1);
     pageBuffer.append("'>上一页</a>");
    }else{
     pageBuffer.append("上一页");
    }
    
    pageBuffer.append("&nbsp;&nbsp;");
    
    //下一页
    if(currentPage<totalPages-1){
     pageBuffer.append("<a href='");
     pageBuffer.append(urlParam);
     pageBuffer.append("&currentPage=");
     pageBuffer.append(currentPage+1);
     pageBuffer.append("'>下一页</a>");
    }else{
     pageBuffer.append("下一页");
    }
    
    pageBuffer.append("&nbsp;&nbsp;");
       
    //尾页
    if(totalPages>1&&currentPage<totalPages-1){
     pageBuffer.append("<a href='");
     pageBuffer.append(urlParam);
     pageBuffer.append("&currentPage=");
     pageBuffer.append(totalPages-1);
     pageBuffer.append("'>尾页</a>");
    }else{
     pageBuffer.append("尾页");
    }
   }
   
  }
  return pageBuffer.toString();
 }
 /**
  * 获取当前页面数
  * @return 当前页面数
  */
 public int getCurrentPage() {
  if(!inited){
   System.out.println("请先调用初始化方法:init(HttpServletRequest request,int allRecord,int pageRows)");
   return 0;
  }
  return currentPage;
 }
}

 

使用方法:创建test.jsp

<%@ page pageEncoding="UTF-8"%>
<jsp:directive.page import="*.PagesUtil"/>
<%

PagesUtil pu = new PagesUtil();
pu .init(request,10000,10);
String urlParam = "./test.jsp?";
%>
<%=pu .getPages(5,urlParam) %>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值