现在的位置: 首页 > 综合 > 正文

通用分页

2013年09月23日 ⁄ 综合 ⁄ 共 4350字 ⁄ 字号 评论关闭

package com.zoe.pager;

public class Page {
 // 1.每页显示数量(everyPage)
 private int everyPage;
 // 2.总记录数(totalCount)
 private int totalCount;
 // 3.总页数(totalPage)
 private int totalPage;
 // 4.当前页(currentPage)
 private int currentPage;
 // 5.起始点(beginIndex)
 private int beginIndex;
 // 6.是否有上一页(hasPrePage)
 private boolean hasPrePage;
 // 7.是否有下一页(hasNextPage)
 private boolean hasNextPage;

 public Page(int everyPage, int totalCount, int totalPage, int currentPage,
   int beginIndex, boolean hasPrePage, boolean hasNextPage) {
  this.everyPage = everyPage;
  this.totalCount = totalCount;
  this.totalPage = totalPage;
  this.currentPage = currentPage;
  this.beginIndex = beginIndex;
  this.hasPrePage = hasPrePage;
  this.hasNextPage = hasNextPage;
 }

 public Page() {
 }

 public int getEveryPage() {
  return everyPage;
 }

 public void setEveryPage(int everyPage) {
  this.everyPage = everyPage;
 }

 public int getTotalCount() {
  return totalCount;
 }

 public void setTotalCount(int totalCount) {
  this.totalCount = totalCount;
 }

 public int getTotalPage() {
  return totalPage;
 }

 public void setTotalPage(int totalPage) {
  this.totalPage = totalPage;
 }

 public int getCurrentPage() {
  return currentPage;
 }

 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }

 public int getBeginIndex() {
  return beginIndex;
 }

 public void setBeginIndex(int beginIndex) {
  this.beginIndex = beginIndex;
 }

 public boolean isHasPrePage() {
  return hasPrePage;
 }

 public void setHasPrePage(boolean hasPrePage) {
  this.hasPrePage = hasPrePage;
 }

 public boolean isHasNextPage() {
  return hasNextPage;
 }

 public void setHasNextPage(boolean hasNextPage) {
  this.hasNextPage = hasNextPage;
 }

}

 

package com.zoe.pager;

public class PageUtil {
 
 public static Page createPage(int everyPage,int totalCount,int currentPage) {
  everyPage = getEveryPage(everyPage);
  currentPage = getCurrentPage(currentPage);
  int totalPage = getTotalPage(everyPage, totalCount);
  int beginIndex = getBeginIndex(everyPage, currentPage);
  boolean hasPrePage = getHasPrePage(currentPage);
  boolean hasNextPage = getHasNextPage(totalPage, currentPage);
  return new Page(everyPage, totalCount, totalPage, currentPage,
    beginIndex, hasPrePage,  hasNextPage);
 }
 
 public static Page createPage(Page page,int totalCount) {
  int everyPage = getEveryPage(page.getEveryPage());
  int currentPage = getCurrentPage(page.getCurrentPage());
  int totalPage = getTotalPage(everyPage, totalCount);
  int beginIndex = getBeginIndex(everyPage, currentPage);
  boolean hasPrePage = getHasPrePage(currentPage);
  boolean hasNextPage = getHasNextPage(totalPage, currentPage);
  return new Page(everyPage, totalCount, totalPage, currentPage,
    beginIndex, hasPrePage,  hasNextPage);
 }

 
 //设置每页显示记录数
 public static int getEveryPage(int everyPage) {
  return everyPage == 0 ? 10 : everyPage;
 }
 
 //设置当前页
 public static int getCurrentPage(int currentPage) {
  return currentPage == 0 ? 1 : currentPage;
 }
 
 //设置总页数,需要总记录数,每页显示多少
 public static int getTotalPage(int everyPage,int totalCount) {
  int totalPage = 0;
  if(totalCount % everyPage == 0) {
   totalPage = totalCount / everyPage;
  } else {
   totalPage = totalCount / everyPage + 1;
  }
  return totalPage;
 }

 //设置起始点,需要每页显示多少,当前页
 public static int getBeginIndex(int everyPage,int currentPage) {
  return (currentPage - 1) * everyPage;
 }

 //设置是否有上一页,需要当前页
 public static boolean getHasPrePage(int currentPage) {
  return currentPage == 1 ? false : true;
 }
 
 //设置是否有下一个,需要总页数和当前页
 public static boolean getHasNextPage(int totalPage, int currentPage) {
  return currentPage == totalPage || totalPage == 0 ? false : true;
 }

}

 

package com.zoe.pager;

import java.util.List;

public class Result {
 private List list;
 private Page page;

 public List getList() {
  return list;
 }

 public void setList(List list) {
  this.list = list;
 }

 public Page getPage() {
  return page;
 }

 public void setPage(Page page) {
  this.page = page;
 }

}

 

<c:choose>
         <c:when test="${page.hasPrePage}">
          <a href="orgAction.action?submitFlag=toList&&currentPage=1">首页</a>
               <a href="orgAction.action?submitFlag=toList&&currentPage=${page.currentPage - 1}&&parentId=${parentId}">上一页</a>
         </c:when>
         <c:otherwise>
          首页
          上一页
         </c:otherwise>
        </c:choose>
             <c:choose>
         <c:when test="${page.hasNextPage}">
          <a href="orgAction.action?submitFlag=toList&&currentPage=${page.currentPage + 1}&&parentId=${parentId}">下一页</a>
               <a href="orgAction.action?submitFlag=toList&&currentPage=${page.totalPage}">尾页</a>
         </c:when>
         <c:otherwise>
          下一页
          尾页
         </c:otherwise>
        </c:choose>
             当前第${page.currentPage}页,共${page.totalPage}页,共有${page.totalCount}条记录

抱歉!评论已关闭.