//分页实体bean
public class Page {
/** imply if the page has previous page */
private boolean hasPrePage;
/** imply if the page has next page */
private boolean hasNextPage;
/** the number of every page */
private int everyPage;
/** the total page number */
private int totalPage;
/** the total record number */
private int totalRecords;
/** the number of current page */
private int currentPage;
/** the begin index of the records by the current query */
private int beginIndex;
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage, int totalRecords,
int currentPage, int beginIndex) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.totalRecords = totalRecords;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
/**
* @param totalRecords
* The totalRecords to set.
*/
public void settotalRecords(int totalRecords)
{
this.totalRecords = totalRecords;
}
/**
* @return Returns the totalRecords.
*
*/
public int getTotalRecords()
{
return this.totalRecords;
}
}
//分页操作类
public class PageUtil {
/**
* Use the origin page to create a new page
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
/**
* the basic page utils not including exception handler
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
if(currentPage >= totalPage) {
beginIndex = getBeginIndex(everyPage, totalPage);
currentPage = totalPage;
}
return new Page(hasPrePage, hasNextPage,
everyPage, totalPage, totalRecords,
currentPage, beginIndex);
}
private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage){
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;
return totalPage;
}
private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}