1. 程式人生 > >struts2 分頁查詢例項

struts2 分頁查詢例項

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;
 }