Since the data required for the corresponding number of pages must be displayed in files such as jsp or html each time, these must be The data is encapsulated in a javabean
. The background will import the queried data into the corresponding javabean object instance. We then store the object in the request scope, html or jsp page
Obtain the required data from the domain
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 + '}'; } }
The above is the detailed content of Java practical html paging design. For more information, please follow other related articles on the PHP Chinese website!