package framework.mybatis.pageAction; import javax.servlet.http.HttpServletRequest; public class PageInfo { public final static String REQUEST_ATTRIBUTE_PAGE_INFO = "REQUEST_ATTRIBUTE_PAGE_INFO"; public final static int DEFAULT_COUNT_PER_PAGE = 10; private int page = 1; private int countPerPage = DEFAULT_COUNT_PER_PAGE; private int totalCount; private int startRow; private int endRow; private int totalPage; protected HttpServletRequest request; protected PageInfo(){ } /** * 设置总条数,计算分页参数 * * @param totalCount * @param countPerPage * @param page */ public void setTotalCount(int totalCount){ this.totalCount = totalCount; if (this.totalCount % this.countPerPage == 0) { totalPage = (int) (this.totalCount / this.countPerPage); } else { totalPage = (int) (this.totalCount / this.countPerPage) + 1; } if(totalPage==0){ totalPage = 1; } if(this.page<1){ this.page=1; } if(this.page>totalPage){ this.page=totalPage; } if (this.totalCount == 0) { startRow=0; endRow=0; }else{ startRow=this.countPerPage*(this.page-1)+1; endRow = this.countPerPage * this.page; endRow = endRow > this.totalCount ? this.totalCount : endRow; } //save to request request.setAttribute(REQUEST_ATTRIBUTE_PAGE_INFO, this); request.setAttribute("pageSearchStartRow", Integer.valueOf(startRow)); } /** * 得到总页数 * @return */ public int getTotalPage(){ return totalPage; } /** * 得到当前页数 * @return */ public int getPage() { return page; } /** * 得到每页多少条 * @return */ public int getCountPerPage() { return countPerPage; } /** * 得到总条数 * @return */ public int getTotalCount() { return totalCount; } /** * 得到开始的行数,从第1行开始,如果没有,则为0 * @return */ public int getStartRow() { return startRow; } /** * 得到结束行数,从第1行开始,如果没有,则为0 * @return */ public int getEndRow() { return endRow; } /** * 设置当前页数 * @param page */ public void setPage(int page) { if(page <1){ page = 1; } this.page = page; } /** * 设置每页条数 * @param countPerPage */ public void setCountPerPage(int countPerPage) { if(countPerPage<1){ countPerPage = 1; } this.countPerPage = countPerPage; } }