分页详讲
来源:互联网 发布:seo搜索引擎是什么 编辑:程序博客网 时间:2024/06/05 18:33
1、建立一个Page类
public class Page { private List records;//放取分页记录 private int currentPageNum;//当前分页 private int pageSize=10;//每页显示的条数 private int totalRecords;//总记录条数 private int totalPage;//总页数 private int startIndex;//每页开始记录的索引 private int prePageNum;//上一页 private int nextPageNum;//下一页nextPageNum private int startPage;//开始页码 private int endPage;//结束页码 private String uri; public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; } public Page(int currentPageNum,int totalRecords) { this.currentPageNum=currentPageNum; this.totalRecords=totalRecords; /** * 记录的总页数 */ totalPage=totalRecords%pageSize==0?totalRecords%pageSize:totalRecords%pageSize+1; //计算每页开始记录的索引 startIndex=(currentPageNum-1)*10; if(totalPage>5) { startPage=currentPageNum-2; endPage=currentPageNum+2; if(startPage<1) { startPage=1; endPage=5; } else if(endPage>totalPage) { endPage=totalPage; startPage=totalPage-4; } }else { startPage=1; endPage=totalPage; } } public List getRecords() { return records; } public void setRecords(List records) { this.records = records; } public int getCurrentPageNum() { return currentPageNum; } public void setCurrentPageNum(int currentPageNum) { this.currentPageNum = currentPageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getStartIdex() { return startIndex; } public void setStartIdex(int startIdex) { this.startIndex = startIdex; } public int getPrePageNum() { prePageNum=currentPageNum-1; if(prePageNum<1) prePageNum=1; return prePageNum; } public void setPrePageNum(int prePageNum) { this.prePageNum = prePageNum; } public int getNextPageNum() { nextPageNum=currentPageNum+1; if(nextPageNum>totalPage) nextPageNum=totalPage; return nextPageNum; } public void setNextPageNum(int nextPageNum) { this.nextPageNum = nextPageNum; } public int getStartPage() { return startPage; } public void setStartPage(int startPage) { this.startPage = startPage; } public int getEndPage() { return endPage; } public void setEndPage(int endPage) { this.endPage = endPage; }}
2、查询数据的总数目
public int getTotalRecords()throws Exception;
3、返回限制查询结果集
public List<类名> findPageRecords(int startIndex,int pageSize)throws Exception;
4、用页数返回结果
//接口 public Page findPage(String num)throws Exception; //实现类 public Page findPage(String num) throws Exception { // TODO Auto-generated method stub int pageNum=1; if(num!=null&&!"".equals(num)) { pageNum=Integer.parseInt(num); } System.out.println("pageNum=="+pageNum); int totalRecords=groupDao.getTotalRecords();//获取记录数 Page page=new Page(pageNum, totalRecords); //System.out.println("page.getStartIdex()=="+page.getStartIdex()); List records=groupDao.findPageRecords(page.getStartIdex(),page.getPageSize()); //System.out.println("records"+records); page.setRecords(records); return page; }
5、前端代码
<!-- 分页开始 --> <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num"> <a href="${pageContext.request.contextPath}${page.uri}&num=${num}">${num}</a> </c:forEach> 第${page.currentPageNum}页/共${page.totalPage }页 <a href="${pageContext.request.contextPath}">首页</a> <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.prePageNum}">上一页</a> <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.nextPageNum}">下一页</a> <a href="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.totalPage}">尾页</a> <!-- 分页结束 --> <select id="pagenum" onchange="jump(this)"> <c:forEach begin="1" end="${page.totalPage}" var="num"> <option value="${num}" ${num==page.currentPageNum?'selected="selected"': ''}>${num}</option> </c:forEach> </select>
阅读全文
0 0
- 分页详讲
- 第五讲 分页的实现
- servlet 第四讲 笔记 分页技术
- (第6讲)servlet 分页技术
- Servlet自学第20讲:分页技术详解
- 磁盘配额详讲
- Floyd算法详讲
- KMP算法详讲
- GridView详讲
- NSBundle详讲
- simpleAdapter详讲
- 详讲jpa
- TCP/IP详讲
- Floyd算法详讲
- 自定义ViewGroup详讲
- Activity基本详讲
- Android 组件详讲
- http协议详讲
- NVIC 的抢占中断和响应中断
- 欢迎使用CSDN-markdown编辑器
- Xcode使用opencv的第一个程序
- 最大子矩阵
- 大数据预科班1、2
- 分页详讲
- C#里partial关键字的作用
- Wormholes
- java开发环境jdk的下载和环境变量的配置
- CodeForces 158 A.Next Round(水~)
- maven环境搭建环境变量配置以及结合myeclipse建立工程
- 剐倘卦端狡斡举盼甲芬释瓷崖衣竿
- Python Plot+Bokeh画图并保存为图片或网页
- 暑期项目实训之健康宝APP(一)