Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.hongqiang.shop.common.persistence; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang3.StringUtils; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; import com.hongqiang.shop.common.config.Global; import com.hongqiang.shop.common.utils.CookieUtils; /** * * @author ThinkGem * @version 2013-7-2 * @param <T> */ public class Page<T> { private int pageNo = 1; // ?? private int pageSize = Integer.valueOf(Global.getConfig("page.pageSize")); // ??-1?? private long count;// -1?? private int first;// private int last;// private int prev;// private int next;// private boolean firstPage;//? private boolean lastPage;//?? private int length = 8;// ? private int slider = 1;// ??? private List<T> list = new ArrayList<T>(); private String orderBy = ""; // updatedate desc, name asc private String funcName = "page"; // ?js??page private String message = ""; // ???n??? /** * * @param request repage ???? * @param response Cookie?? */ public Page(HttpServletRequest request, HttpServletResponse response) { this(request, response, -2); } /** * * @param request repage ???? * @param response Cookie?? * @param pageSize ? -1 ?? */ public Page(HttpServletRequest request, HttpServletResponse response, int pageSize) { // ??repage???? String no = request.getParameter("pageNo"); if (StringUtils.isNumeric(no)) { CookieUtils.setCookie(response, "pageNo", no); this.setPageNo(Integer.parseInt(no)); } else if (request.getParameter("repage") != null) { no = CookieUtils.getCookie(request, "pageNo"); if (StringUtils.isNumeric(no)) { this.setPageNo(Integer.parseInt(no)); } } // ???repage????? String size = request.getParameter("pageSize"); if (StringUtils.isNumeric(size)) { CookieUtils.setCookie(response, "pageSize", size); this.setPageSize(Integer.parseInt(size)); } else if (request.getParameter("repage") != null) { no = CookieUtils.getCookie(request, "pageSize"); if (StringUtils.isNumeric(size)) { this.setPageSize(Integer.parseInt(size)); } } if (pageSize != -2) { this.pageSize = pageSize; } // ?? String orderBy = request.getParameter("orderBy"); if (StringUtils.isNotBlank(orderBy)) { this.setOrderBy(orderBy); } } /** * * @param pageNo ?? * @param pageSize ? */ public Page(int pageNo, int pageSize) { this(pageNo, pageSize, 0); } /** * * @param pageNo ?? * @param pageSize ? * @param count ?? */ public Page(int pageNo, int pageSize, long count) { this(pageNo, pageSize, count, new ArrayList<T>()); } /** * * @param pageNo ?? * @param pageSize ? * @param count ?? * @param list ? */ public Page(int pageNo, int pageSize, long count, List<T> list) { this.setCount(count); this.setPageNo(pageNo); this.pageSize = pageSize; this.setList(list); } /** * ?? */ public void initialize() { //1 this.first = 1; this.last = (int) (count / (this.pageSize < 1 ? 20 : this.pageSize) + first - 1); if (this.count % this.pageSize != 0 || this.last == 0) { this.last++; } if (this.last < this.first) { this.last = this.first; } if (this.pageNo <= 1) { this.pageNo = this.first; this.firstPage = true; } if (this.pageNo >= this.last) { this.pageNo = this.last; this.lastPage = true; } if (this.pageNo < this.last - 1) { this.next = this.pageNo + 1; } else { this.next = this.last; } if (this.pageNo > 1) { this.prev = this.pageNo - 1; } else { this.prev = this.first; } //2 if (this.pageNo < this.first) {// ?? this.pageNo = this.first; } if (this.pageNo > this.last) {// ? this.pageNo = this.last; } } /** * ? * <div class="page">${page}</div> */ @Override public String toString() { initialize(); StringBuilder sb = new StringBuilder(); if (pageNo == first) {// sb.append("<li class=\"disabled\"><a href=\"javascript:\">« </a></li>\n"); } else { sb.append("<li><a href=\"javascript:" + funcName + "(" + prev + "," + pageSize + ");\">« </a></li>\n"); } int begin = pageNo - (length / 2); if (begin < first) { begin = first; } int end = begin + length - 1; if (end >= last) { end = last; begin = end - length + 1; if (begin < first) { begin = first; } } if (begin > first) { int i = 0; for (i = first; i < first + slider && i < begin; i++) { sb.append("<li><a href=\"javascript:" + funcName + "(" + i + "," + pageSize + ");\">" + (i + 1 - first) + "</a></li>\n"); } if (i < begin) { sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n"); } } for (int i = begin; i <= end; i++) { if (i == pageNo) { sb.append("<li class=\"active\"><a href=\"javascript:\">" + (i + 1 - first) + "</a></li>\n"); } else { sb.append("<li><a href=\"javascript:" + funcName + "(" + i + "," + pageSize + ");\">" + (i + 1 - first) + "</a></li>\n"); } } if (last - end > slider) { sb.append("<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n"); end = last - slider; } for (int i = end + 1; i <= last; i++) { sb.append("<li><a href=\"javascript:" + funcName + "(" + i + "," + pageSize + ");\">" + (i + 1 - first) + "</a></li>\n"); } if (pageNo == last) { sb.append("<li class=\"disabled\"><a href=\"javascript:\"> »</a></li>\n"); } else { sb.append("<li><a href=\"javascript:" + funcName + "(" + next + "," + pageSize + ");\">" + " »</a></li>\n"); } sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">? "); sb.append("<input type=\"text\" value=\"" + pageNo + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)"); sb.append(funcName + "(this.value," + pageSize + ");\" onclick=\"this.select();\"/> / "); sb.append("<input type=\"text\" value=\"" + pageSize + "\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)"); sb.append(funcName + "(" + pageNo + ",this.value);\" onclick=\"this.select();\"/> ?"); sb.append(" " + count + " ?" + (message != null ? message : "") + "</a><li>\n"); sb.insert(0, "<ul>\n").append("</ul>\n"); sb.append("<div style=\"clear:both;\"></div>"); // sb.insert(0,"<div class=\"page\">\n").append("</div>\n"); return sb.toString(); } // public static void main(String[] args) { // Page<String> p = new Page<String>(3, 3); // System.out.println(p); // System.out.println(""+p.getFirst()); // System.out.println(""+p.getLast()); // System.out.println(""+p.getPrev()); // System.out.println(""+p.getNext()); // } /** * ? * @return */ public long getCount() { return count; } /** * ? * @param count */ public void setCount(long count) { this.count = count; if (pageSize >= count) { pageNo = 1; } } /** * ??? * @return */ public int getPageNo() { return pageNo; } /** * ?? * @param pageNo */ public void setPageNo(int pageNo) { this.pageNo = pageNo; } /** * ??? * @return */ public int getPageSize() { return pageSize; } /** * ??500 * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize <= 0 ? 10 : pageSize > 500 ? 500 : pageSize; } /** * * @return */ public int getFirst() { return first; } /** * * @return */ public int getLast() { return last; } /** * ?? * @return getLast(); */ public int getTotalPage() { return getLast(); } /** * ? * @return */ public boolean isFirstPage() { return firstPage; } /** * ?? * @return */ public boolean isLastPage() { return lastPage; } /** * * @return */ public int getPrev() { if (isFirstPage()) { return pageNo; } else { return pageNo - 1; } } /** * * @return */ public int getNext() { if (isLastPage()) { return pageNo; } else { return pageNo + 1; } } /** * ?? * @return List<T> */ public List<T> getList() { return list; } /** * ? * @param list */ public void setList(List<T> list) { this.list = list; } /** * ?? * @return */ public String getOrderBy() { return orderBy; } /** * ? updatedate desc, name asc */ public void setOrderBy(String orderBy) { this.orderBy = orderBy; } /** * ??js?? * function ${page.funcName}(pageNo){location="${ctx}/list-${category.id}${urlSuffix}?pageNo="+i;} * @return */ public String getFuncName() { return funcName; } /** * ?js??page * @param funcName page */ public void setFuncName(String funcName) { this.funcName = funcName; } /** * ???n??? * @param message */ public void setMessage(String message) { this.message = message; } /** * ? * @return this.pageSize==-1 */ public boolean isDisabled() { return this.pageSize == -1; } /** * ? * @return this.count==-1 */ public boolean isNotCount() { return this.count == -1; } /** * ? Hibernate FirstResult */ public int getFirstResult() { int firstResult = (getPageNo() - 1) * getPageSize(); if (firstResult >= getCount()) { firstResult = 0; } return firstResult; } /** * ? Hibernate MaxResults */ public int getMaxResults() { return getPageSize(); } /** * ? Spring data JPA */ public Pageable getSpringPage() { List<Order> orders = new ArrayList<Order>(); if (orderBy != null) { for (String order : StringUtils.split(orderBy, ",")) { String[] o = StringUtils.split(order, " "); if (o.length == 1) { orders.add(new Order(Direction.ASC, o[0])); } else if (o.length == 2) { if ("DESC".equals(o[1].toUpperCase())) { orders.add(new Order(Direction.DESC, o[0])); } else { orders.add(new Order(Direction.ASC, o[0])); } } } } return new PageRequest(this.pageNo - 1, this.pageSize, new Sort(orders)); } /** * Spring data JPA ? */ public void setSpringPage(org.springframework.data.domain.Page<T> page) { this.pageNo = page.getNumber(); this.pageSize = page.getSize(); this.count = page.getTotalElements(); this.list = page.getContent(); } }