Page分页

来源:互联网 发布:知乎暨南大学临床医学 编辑:程序博客网 时间:2024/05/18 20:51
Page.java:
package com.qddx.wmal.dao.impl;


public class Page {


private int everyPage; // 每页显示数量
private int totalCount; // 总记录数
private int totalPage; // 总页数
private int currentPage; // 当前页码
private int beginIndex; // 起始点
private boolean hasPrePage; // 是否有上一页
private boolean hasNextPage; // 是否有下一页


public Page(int everyPage, int totalCount, int totalPage, int currentPage,
int beginIndex, boolean hasPrePage, boolean hasNextPage) {
super();
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;
}


}




PageUtil.java


package com.qddx.wmal.dao.impl;


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 int getEveryPage(int everyPage){
return 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 totalPage == currentPage || totalPage == 0 ? false : true; 
}
}


Action.java


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


public int getCurrentPage() {
return currentPage;
}
public String showCartes() throws Exception{
List<Carte> cartes = carteHibernate.findByRes((Res) session.get("res"));
Page page = PageUtil.createPage(everyPage, cartes.size(), currentPage);
int endIndex = page.getBeginIndex() + page.getEveryPage();
if(endIndex > page.getTotalCount()){
endIndex = page.getTotalCount();
}
cartes = cartes.subList(page.getBeginIndex(), endIndex);
ServletActionContext.getRequest().setAttribute("cartes", cartes);
ServletActionContext.getRequest().setAttribute("page", page);
return this.SUCCESS;
}
0 0
原创粉丝点击