java 分页公用类
jopen
12年前
package com.cn.ohd.face.payment.pageutil; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import javax.servlet.http.HttpServletRequest; import java.util.Map; public class PageNumber<E> { private int totals; private List<E> bigList; //总记录集 private List<E> smallList; //当页显示的记录集 private int currentPage = 1; //当前页 private int pages; //总页数 private int recorderPage =10; //每页记录数 private int privious; //上一页 private int next; //下一页 private boolean firstPage;//第一页 private boolean lastPage; //最后一页 private String url; public PageNumber(HttpServletRequest request, List bigList) { //根据大List获得总记录数和总页数 this.bigList = bigList; totals = bigList.size(); if (totals % recorderPage == 0) { pages = totals / recorderPage; } else { pages = totals / recorderPage + 1; } //从请求中获得要显示的页码 String page = request.getParameter("pageNo"); if(page!=null && !"".equals(page)) { currentPage=Integer.parseInt(page); } //根据页码获得小List if (currentPage == 1) { firstPage = true; } else { firstPage = false; } if (currentPage == pages) { lastPage = true; } else { lastPage = false; } privious = currentPage - 1; next = currentPage + 1; this.smallList = new ArrayList<E>(); for (int i = (currentPage - 1) * recorderPage; i < currentPage * recorderPage && i < totals; i++) { smallList.add((E) bigList.get(i)); } //设置URL参数 Enumeration en = request.getParameterNames(); Map map = request.getParameterMap(); StringBuffer sb = new StringBuffer(); sb.append(request.getRequestURI()); int i=0; while(en.hasMoreElements()) { String paramName = (String)en.nextElement(); if(!paramName.equals("pageNo")) { if(i==0) { sb.append("?"); }else{ sb.append("&"); } sb.append(paramName).append("="); String[] value = (String[])map.get(paramName); sb.append(value[0]); i++; } } this.url = sb.toString(); } public String getUrl() { return url; } public void setUrl(HttpServletRequest request) { this.url = request.getRequestURI(); } public boolean isFirstPage() { return firstPage; } public void setFirstPage(boolean firstPage) { this.firstPage = firstPage; } public boolean isLastPage() { return lastPage; } public void setLastPage(boolean lastPage) { this.lastPage = lastPage; } public int getNext() { return next; } public void setNext(int next) { this.next = next; } public int getPrivious() { return privious; } public void setPrivious(int privious) { this.privious = privious; } public List<E> getBigList() { return bigList; } public void setBigList(List<E> bigList) { this.bigList = bigList; totals = bigList.size(); if (totals % recorderPage == 0) { pages = totals / recorderPage; } else { pages = totals / recorderPage + 1; } } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int a) { this.currentPage = a; if (currentPage == 1) { firstPage = true; } else { firstPage = false; } if (currentPage == pages) { lastPage = true; } else { lastPage = false; } privious = a - 1; next = a + 1; this.smallList = new ArrayList<E>(); for (int i = (currentPage - 1) * recorderPage; i < currentPage * recorderPage && i < totals; i++) { smallList.add(bigList.get(i)); } } public int getPages() { return pages; } public void setPages(int pages) { this.pages = pages; } public List<E> getSmallList() { return smallList; } public void setSmallList(List<E> smallList) { this.smallList = smallList; } public int getTotals() { return totals; } public void setTotals(int totals) { this.totals = totals; } public int getRecorderPage() { return recorderPage; } public void setRecorderPage(int recorderPage) { this.recorderPage = recorderPage; } public void clearBigList() { this.bigList = null; } }来自:http://www.cnblogs.com/leess/archive/2012/11/17/2774503.html