由于每次要将对应页数所需要的数据在jsp或者html等文件中显示出来,所以要将这些数据封装在一个javabean中
,后台都将查询到的数据导入到对应的javabean对象实例中,我们再将该对象存入request作用域,html或者jsp页面
从域中获取所需要的数据
import java.util.List; public class LepPage<T> { /** * 如果指定的分页大小小于等于0,则义默认分页为20条数据 */ public static final int DEFAULT_ROW_SIZE = 20; /** * 如果指定的分页页码小于等于0,则默认为第一页。 */ public static final int DEFAULT_PAGE_NUM = 1; private int pageNo=DEFAULT_PAGE_NUM; // 当前页码 private int pageSize=DEFAULT_ROW_SIZE; // 页面大小 private int totalCount=-1; // 总记录数 private int totalPage=-1; // 总页数 private int startIndex; // 分页开始位置 private int endIndex; // 分页结束位置 private List<T> results; // 返回的结果集 /* * 是否手动设置过总记录数<br/> * 如果手动设置过,则不自动去计算数据总数。否则,将对SQL语句包装成COUNT语句去查询总记录数 */ private boolean alreadySetTotolRecode = false; /* * 计算总页数 */ private void calculateTotalPage() { this.alreadySetTotolRecode = true; if ( this.totalCount < 0 ) { this.totalCount = 0; } if ( this.totalCount % this.pageSize == 0 ) { this.totalPage = this.totalCount / this.pageSize; } else { this.totalPage = this.totalCount / this.pageSize + 1; } } /** * 该方法不推荐使用,使用{@code Page(int PageNo, int pageSize)}进行替换 */ public LepPage() { super(); } private void calculatorPageNo() { this.startIndex = pageNo > 0 ? ((pageNo - 1) * pageSize) : 0; this.endIndex = pageNo * pageSize; } /** * 构造一个Page对象 * @param pageNo 当前页码 * @param pageSize 页面大小 */ public LepPage(int pageNo, int pageSize) { this.pageNo = pageNo; this.pageSize = pageSize; calculatorPageNo(); } public int getPageNo() { return pageNo; } public void setPageNo(int PageNo) { if ( PageNo <= 0 ) { this.pageNo = DEFAULT_PAGE_NUM; } this.pageNo = PageNo; calculatorPageNo(); } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if ( pageSize <= 0 ) { this.pageSize = DEFAULT_ROW_SIZE; } this.pageSize = pageSize; calculatorPageNo(); } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; // 去计算总页数 calculateTotalPage(); } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public int getEndIndex() { return endIndex; } public void setEndIndex(int endIndex) { this.endIndex = endIndex; } public List<T> getResults() { return results; } public void setResults(List<T> results) { this.results = results; } public boolean isAlreadySetTotolRecode() { return alreadySetTotolRecode; } public void setAlreadySetTotolRecode(boolean alreadySetTotolRecode) { this.alreadySetTotolRecode = alreadySetTotolRecode; } @Override public String toString() { return "Page{" + "pageNo=" + pageNo + ", pageSize=" + pageSize + ", totalCount=" + totalCount + ", totalPage=" + totalPage + ", startIndex=" + startIndex + ", endIndex=" + endIndex + ", results=" + results + ", alreadySetTotolRecode=" + alreadySetTotolRecode + '}'; } }
Atas ialah kandungan terperinci Java实战之html分页设计. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!