package com.jmail.code.util; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Enumeration; import java.util.List; import javax.servlet.http.HttpServletRequest; /** * @author zero * * <pre> * 分页类 * 使用:new Page(request).setRowsCount(120).useGroupPage(10).setNextPage(" >>").setPrePage("<< ").buildPagination(); * 控制器中可以使用Page 提供的getParameterValue() 等方法获取传来的数据 * * 获取总记录数:${rowsCount} * 获取总页数:${totalPages} * 获取当前页数:${p} * 获取当前页第一条记录序号:${firstRow} * 获取分页代码:${pagesUrl} * 获取Page对象:${page} * * 定义样式参考: * .currentPage: 定义当前页样式 * .otherPage: 定义非当前页样式 * .prePage: 上一页 * .nextPage: 下一页 * </pre> */ public class Page { private static final String PAGE = "p"; public static final String ENCODING_UTF8 = "utf-8"; public static final String ENCODING_GBK = "gbk"; public static final int PAGESIZE_5 = 5; public static final int PAGESIZE_10 = 10; public static final int PAGESIZE_15 = 15; public static final int PAGESIZE_20 = 20; public static final int PAGESIZE_30 = 30; public static final int PAGESIZE_50 = 50; public static final int PAGESIZE_100 = 100; // private boolean useEncoding=false; private String encoding = ENCODING_UTF8; private int totalPages; private int currentPage; private StringBuilder pagesUrl = new StringBuilder(); // 配置项 private int pageSize = PAGESIZE_10; private int groupPageSize = 5; private boolean useGroupPage = false; private HttpServletRequest request; private int rowsCount; private int currentPageFirstRow; // 显示文本 private String prePage = "上一页"; private String nextPage = "下一页"; private String firstPage = "首页"; private String lastPage = "尾页"; private boolean displayPreAndNextPageLink = false; // 当前页数据 private List<Object> data; public Page(HttpServletRequest request) { this.init(request); this.request = request; } /** * @param rowsCount * 总记录数 * @param request * HttpServletRequest */ public Page(int rowsCount, HttpServletRequest request) { this.init(request); this.rowsCount = rowsCount; this.request = request; } private void init(HttpServletRequest request) { String cp = request.getParameter(PAGE); if (cp == null) { request.setAttribute(PAGE, 1); this.currentPage = 1; } else { this.currentPage = Integer.parseInt(request.getParameter(PAGE)); } } public boolean isDisplayPreAndNextPageLink() { return displayPreAndNextPageLink; } /** * 当当前页面为第一页时,是否显示"上一页". * * @return */ public Page displayPreAndNextPageLink() { this.displayPreAndNextPageLink = true; return this; } public HttpServletRequest getRequest() { return request; } public String getPagesUrl() { return this.pagesUrl.toString(); } public int getTotalPages() { return totalPages; } public int getCurrentPage() { return currentPage; } public int getPageSize() { return pageSize; } public int getRowsCount() { return rowsCount; } public Page setRowsCount(int rowsCount) { this.rowsCount = rowsCount; return this; } public int getGroupPageSize() { return groupPageSize; } public boolean isGroupPage() { return useGroupPage; } public Page setPageSize(int pageSize) { this.pageSize = pageSize; return this; } /** * 使用组分,默认不使用. * * @return */ public Page useGroupPage() { this.useGroupPage = true; return this; } public Page useGroupPage(int size) { this.useGroupPage = true; this.groupPageSize = size; return this; } public int getCurrentPageFirstRow() { return currentPageFirstRow = (this.currentPage - 1) * pageSize; } /** * 设置上一页的显示文本 * * @param prePage */ public Page setPrePage(String prePage) { this.prePage = prePage; return this; } /** * 设置下一页的显示文本 * * @param nextPage */ public Page setNextPage(String nextPage) { this.nextPage = nextPage; return this; } /** * 同页面编码,默认为UTF-8 * * @param encoding * @return */ public Page useEncoding(String encoding) { this.encoding = encoding; return this; } public void setData(List<Object> data) { this.data = data; } public void buildPagination() { this.totalPages = (rowsCount + pageSize - 1) / pageSize; StringBuilder next = new StringBuilder(); StringBuilder pre = new StringBuilder(); StringBuilder otherPageLinks = null; StringBuilder url = new StringBuilder(128); String baseUrl = request.getRequestURI(); // 构造查询字符串 StringBuilder query = new StringBuilder(); query.append(baseUrl); query.append("?"); Enumeration<?> enume = request.getParameterNames(); try { while (enume.hasMoreElements()) { String parName = (String) enume.nextElement(); if ("uc".equalsIgnoreCase(parName)) continue; if (!PAGE.equalsIgnoreCase(parName)) { String v = request.getParameter(parName); if (v == null || v.trim().length() == 0) continue; String value = getParameterValue(parName); query.append(parName); query.append("="); query.append(URLEncoder.encode(value, encoding)); query.append("&"); request.setAttribute(parName, value);// ... } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } query.append("uc=y&p="); url.append(query); if (this.currentPage > this.totalPages || this.currentPage < 1) { this.currentPage = 1; } // 下一页 if (this.currentPage > 0 && this.currentPage < totalPages) { next.append("<a href='").append(url).append(this.currentPage + 1) .append("' class='nextPage'>").append(nextPage) .append("</a>"); } else { if (this.displayPreAndNextPageLink && totalPages > 1) { next.append("<a>").append(this.nextPage).append("</a>"); } } // 上一页 if (this.currentPage > 1 && this.currentPage <= totalPages) { pre.append("<a href='").append(url).append(this.currentPage - 1) .append("' class='prePage'>").append(prePage) .append("</a>"); } else { if (this.displayPreAndNextPageLink && totalPages > 1) { pre.append("<a>").append(this.prePage).append("</a>"); } } this.pagesUrl.append(pre);// 上一页 // -----------------------组分页------------------ if (useGroupPage && this.totalPages > 1) { otherPageLinks = new StringBuilder(64); if (this.groupPageSize > this.totalPages) { this.groupPageSize = this.totalPages; } int tmp = groupPageSize / 2; int leftFirst = 1; if (this.currentPage > tmp + 1) { leftFirst = this.currentPage - tmp;// 最左边页码 } if (this.currentPage + tmp > this.totalPages) { leftFirst = this.totalPages - this.groupPageSize + 1;// 最左边页码 } for (int i = leftFirst; i < this.groupPageSize + leftFirst; i++) { if (i == this.currentPage) { otherPageLinks.append("<a class='currentPage'><span>") .append(i).append("</span></a>"); } else if (i <= this.totalPages) { otherPageLinks.append("<a href='").append(url).append(i) .append("' class='otherPage'><span>").append(i) .append("</span></a>"); } } this.pagesUrl.append(otherPageLinks);// 组分页 } this.pagesUrl.append(next);// 下一页 currentPageFirstRow = (this.currentPage - 1) * pageSize; request.setAttribute(PAGE, this.currentPage); request.setAttribute("firstRow", this.currentPageFirstRow); request.setAttribute("totalPages", this.totalPages); request.setAttribute("pageSize", this.pageSize); request.setAttribute("rowsCount", this.rowsCount); request.setAttribute("pagesUrl", this.pagesUrl); System.out.println(pagesUrl); request.setAttribute("data", data); request.setAttribute("page", this); } public String getParameterValue(String name) { return Page.getParameterValue(request, name, encoding); } public static String getParameterValue(HttpServletRequest request,String name) { return Page.getParameterValue(request, name, ENCODING_UTF8); } public static String getParameterValue(HttpServletRequest request, String name, String encoding) { if (name == null) return null; String value = request.getParameter(name); if (value == null) return null; if ("post".equalsIgnoreCase(request.getMethod())) { return value.trim(); } else { try { return new String(value.trim().getBytes("iso8859-1"), encoding); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }// ..... return null; } }