由於每次要將對應頁數所需的資料在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 + '}'; } }
以上是Java實戰html分頁設計的詳細內容。更多資訊請關注PHP中文網其他相關文章!