java 分页_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java 分页

java 分页

 2012/6/29 16:37:00  zisetoufa  程序员俱乐部  我要评论(0)
  • 摘要:publicclassPager{privateintcurrentPage=1;//当前页privateinttotalPages=0;//总页数privateintshowPages=10;//分页要显示的页数privateintpageRecorders=2;//每页记录privateinttotalRows=0;//总行数privateintpageStartRow=0;//每页的起始行privateintpageEndRow=0
  • 标签:Java
public class Pager {

private int currentPage = 1;// 当前页
private int totalPages = 0;// 总页数
private int showPages = 10;// 分页要显示的页数
private int pageRecorders = 2;// 每页记录
private int totalRows = 0;// 总行数
private int pageStartRow = 0;// 每页的起始行
private int pageEndRow = 0;// 每页显示的终止数
private boolean hasNextPage = false;// 没有下一页
private boolean hasPreviousPage = false;// 没有上一页

private List list;// 传过一个list,就可以对list进行分页
private String url;// action跳转页面

public void ShowByPageImpl(List list, String url) {
this.list = list;
this.url = url;
totalRows = list.size();
hasPreviousPage = false;
currentPage = 1;
if (totalRows % pageRecorders == 0) {
totalPages = totalRows / pageRecorders;
} else {
totalPages = totalRows / pageRecorders + 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if (totalRows < pageRecorders) {
this.pageStartRow = 0;
this.pageEndRow = totalRows;
} else {
this.pageStartRow = 0;
this.pageEndRow = pageRecorders;
}
}

public int getCurrentPage() {
return currentPage;
}

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

public int getTotalPages() {
return totalPages;
}

public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}

public int getPageRecorders() {
return pageRecorders;
}

public void setPageRecorders(int pageRecorders) {
this.pageRecorders = pageRecorders;
}

public int getTotalRows() {
return totalRows;
}

public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

public int getPageStartRow() {
return pageStartRow;
}

public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}

public int getPageEndRow() {
return pageEndRow;
}

public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}

public boolean isHasNextPage() {
return hasNextPage;
}

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

public boolean isHasPreviousPage() {
return hasPreviousPage;
}

public void setHasPreviousPage(boolean hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}

public List getList() {
return list;
}

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

// 首页
public void getFirstPage() {
currentPage = 1;
hasPreviousPage = false;
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}

// 下一页
public void getNextPage() {
currentPage = currentPage + 1;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
}

// 上一页
public void getPreviousPage() {
currentPage = currentPage - 1;
if (currentPage == 0) {
currentPage = 1;
}
if (currentPage >= totalPages) {
hasNextPage = false;
} else {
hasNextPage = true;
}
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
}

// 最后一页
public void getLastPage() {
currentPage = totalPages;
if (currentPage == 0) {
currentPage = 1;
}
hasNextPage = false;
if ((currentPage - 1) > 0) {
hasPreviousPage = true;
} else {
hasPreviousPage = false;
}
}

// 获取选择页
public void getSelectPage(int selectPage) {
currentPage = selectPage;
if (currentPage >= totalPages)
hasNextPage = false;
else
hasNextPage = true;
if ((currentPage - 1) > 0)
hasPreviousPage = true;
else
hasPreviousPage = false;
}

// 获取当前列表
public List getPageList() {
if (currentPage * pageRecorders < totalRows) {
pageEndRow = currentPage * pageRecorders;
pageStartRow = pageEndRow - pageRecorders;
} else {
pageEndRow = totalRows;
pageStartRow = pageRecorders * (totalPages - 1);
}
List templist = new ArrayList();
for (int j = 0, i = pageStartRow; i < pageEndRow; j++, i++)
templist.add(j, list.get(i));
return templist;
}

// 控制页面跳转
public void controlPage(String action) {
if (action.equals("firstPage"))
this.getFirstPage();
else if (action.equals("nextPage"))
this.getNextPage();
else if (action.equals("previousPage"))
this.getPreviousPage();
else if (action.equals("lastPage"))
this.getLastPage();
else
this.getSelectPage(Integer.valueOf(action));
}
}

action中调用:

Pager pager = new Pager();
int currentPage = Integer.parseInt(request.getParameter("currentPage"));//客户端当前显示的页数
int contentNum = Integer.parseInt(request.getParameter("contentNum"));//每页显示的内容条数
pager.ShowByPageImpl(productList, currentPage, contentNum);//分页处理
List listPage = pager.getPageList(currentPage, contentNum, productList.size());//获取当前列表
int totalPage = pager.getTotalPages();//获得总页数
发表评论
用户名: 匿名