Java分页

来源:互联网 发布:江山人才招聘21js 编辑:程序博客网 时间:2024/05/17 06:49

之前找分页不方便, 就自己写了个,代码如下:

package com.vcr.util;

import java.util.List;
import java.util.Map;

public class Pager {

 int curPage = 1;// 当前页
 int totoalPage = 0;// 总页数
 int prePage = 0;// 上一页
 int nextPage = 0;// 下一页
 int totoalCnt = 0;// 总条数
 int cntOfPage = 10;// 每页的条数
 String sorder = "desc";//排序方式
 String sindex = "";//排序字段
 List<Map<String, Object>> rows;//保存实际的数据

 public String getSorder() {
  return sorder;
 }

 public void setSorder(String sorder) {
  this.sorder = sorder;
 }

 public String getSindex() {
  return sindex;
 }

 public void setSindex(String sindex) {
  this.sindex = sindex;
 }

 /**
  * 注意该方法的返回值:List。实际上包含了实际的数据,
  * 而这些数据是放在Map<String, Object>中的。
  * 因而,子类在action方法如:execute中,应该构建一个
  * Map<String, Object>对象,将数据放入其中,并把该对象放入
  * List中。
  * @return
  */
 public List<Map<String, Object>> getRows() {
  return rows;
 }

 public void setRows(List<Map<String, Object>> rows) {
  this.rows = rows;
 }

 public void calTotoalPage() {// 计算总页数
  if (totoalCnt % cntOfPage > 0) {
   this.totoalPage = totoalCnt / cntOfPage + 1;
  } else {
   this.totoalPage = totoalCnt / cntOfPage;
  }
 }

 public int getTotoalCnt() {
  return totoalCnt;
 }

 public void setTotoalCnt(int totoalCnt) {
  this.totoalCnt = totoalCnt;
  calTotoalPage();
  setNextPage();
  setPrePage();
 }

 public int getCurPage() {
  return curPage;
 }

 public void setCurPage(int curPage) {
  this.curPage = curPage;
  setNextPage();
  setPrePage();
 }

 public int getNextPage() {
  return this.nextPage;
 }

 public void setNextPage() {

  if (this.curPage < this.totoalPage) {
   this.nextPage = this.curPage + 1;
  } else {
   this.nextPage = this.totoalPage;
  }
  if (this.nextPage == 0) {
   this.nextPage = 1;
  }
 }

 public int getPrePage() {
  return this.prePage;
 }

 public void setPrePage() {
  if (this.curPage > 1) {
   this.prePage = this.curPage - 1;
  } else {
   this.prePage = 1;
  }
 }

 public int getTotoalPage() {
  return totoalPage == 0 ? 1 : totoalPage;
 }

 public void setTotoalPage(int totoalPage) {
  this.totoalPage = totoalPage;

 }

 public int getCntOfPage() {
  return cntOfPage;
 }

 public void setCntOfPage(int cntOfPage) {
  this.cntOfPage = cntOfPage;
  calTotoalPage();
 }

}

 

原创粉丝点击