收藏 分销(赏)

分页的实现原理-分页的实现步骤.doc

上传人:仙人****88 文档编号:12072920 上传时间:2025-09-06 格式:DOC 页数:6 大小:77KB 下载积分:10 金币
下载 相关 举报
分页的实现原理-分页的实现步骤.doc_第1页
第1页 / 共6页
分页的实现原理-分页的实现步骤.doc_第2页
第2页 / 共6页


点击查看更多>>
资源描述
分页的实现原理: 1. 获得需要显示的总的记录数rowCount—》从数据库中取 2. 设定每页最多显示的记录数size—》10 3. 指定显示的页码:num à作为参数得到 4. 根据rowCount,size,num可计算出其余的元素: a) 本页面从多少行记录开始:startRow = (this.num-1) * size ; b) 共有多少页:pageCount = (int) Math.ceil((double)rowCount/size); c) 下一页:next=Math.min( this.pageCount, this.num+1) d) 上一页:prev = Math.max(1 , this.num-1) e) 页号控制元素: numCount:每页最多显示多少页号。(一共显示numCount+1个页号) start = Math.max(this.num-numCount/2, first); //本页显示页号从多少页开始 end = Math.min(start+numCount, last); //本页显示页号在多少页结束 页号控制: if(end-start < numCount){ //当本页总显示的页号数不够numCount时,如何计算起始页号。 start = Math.max(end-numCount, 1); } 分页实现步骤: 1. 将Page类引入。需要自己修改的可自行修改。 package com.puckasoft.video.util; public class Page { private int num; //当前页号, 采用自然数计数 1,2,3,... private int size; //页面大小:一个页面显示多少个数据 private int rowCount;//数据总数:一共有多少个数据 private int pageCount; // 页面总数 private int startRow;//当前页面开始行, 第一行是0行 private int first = 1;//第一页 页号 private int last;//最后页 页号 private int next;//下一页 页号 private int prev;//前页 页号 private int start;//页号式导航, 起始页号 private int end;//页号式导航, 结束页号 private int numCount = 10;//页号式导航, 最多显示页号数量为numCount+1;这里显示11页。 public Page(int size, String str_num, int rowCount) { int num = 1; if (str_num != null) { num = Integer.parseInt(str_num); } this.num = num; this.size=size; this.rowCount = rowCount; this.pageCount = (int) Math.ceil((double)rowCount/size); this.num = Math.min(this.num, pageCount); this.num = Math.max(1, this.num); this.startRow = (this.num-1) * size ; this.last = this.pageCount; this.next = Math.min( this.pageCount, this.num+1); this.prev = Math.max(1 , this.num-1); //计算page 控制 start = Math.max(this.num-numCount/2, first); end = Math.min(start+numCount, last); if(end-start < numCount){ start = Math.max(end-numCount, 1); } } // 为了节省篇幅,get,set方法省略。 } 2. 引入fenye.jsp / pagination.jsp文件: <%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%> <%@ taglib prefix="c" uri=" <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> </head> <body> <% String url4page = request.getParameter("url4page").trim(); url4page = (url4page.indexOf("?")==-1)?url4page+"?num=":url4page+"&num="; %> <c:choose> <c:when test="${page.num != 1}"> <a href="<%=url4page %>${page.first}">首页</a> &nbsp;&nbsp; <a href="<%=url4page %>${page.prev}">前一页</a> &nbsp;&nbsp; </c:when> <c:otherwise> <b>首页</b> &nbsp;&nbsp; <b>前一页</b> &nbsp;&nbsp; </c:otherwise> </c:choose> <c:forEach var="i" begin="${page.start}" end="${page.end}" step="1"> <c:choose> <c:when test="${page.num != i}"> <a href="<%=url4page %>${i}"><b>[${i}]</b> </a> &nbsp;&nbsp; </c:when> <c:otherwise> <b>[${i}]</b> &nbsp;&nbsp; </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="${page.num != page.pageCount}"> <a href="<%=url4page %>${page.next}">后一页</a> &nbsp;&nbsp; <a href="<%=url4page %>${page.last}">末页</a> &nbsp;&nbsp; </c:when> <c:otherwise> <b>末页</b> &nbsp;&nbsp; <b>后一页</b> &nbsp;&nbsp; </c:otherwise> </c:choose> 共${page.pageCount}页 <br /> </body> </html> 3. 在相应servlet/jsp 里面,将需要显示的记录放到list里面,并将list放到request或者session里面:相应servlet写法如下: <%@ page language="java" import="java.util.*" pageEncoding="GBK" %> <%@ page import="com.puckasoft.video.dao.*,com.puckasoft.video.vo.*,com.puckasoft.video.util.*" %> <%@ taglib prefix="c" uri=" <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <% //处理总记录数 int rowCount = VideoDao.countAllVideos(); //将Page类放入作用域,以便在jsp页面中显示 Page p1注意这里不能叫page,因为page为内置对象。 = new Page(10, request.getParameter("num"), rowCount); request.setAttribute("page", p1); //将视频信息放入作用域,以便在页面中显示 List list = VideoDao.listAllVideos(p1.getStartRow(), p1.getSize()); request.setAttribute("vList", list); %> <jsp:include flush="true" page="head.jsp"></jsp:include> <h1>将会列出所有的视频</h1> <c:forEach items="${requestScope.vList}" var="video"> <a href="display.jsp?vname=${video.vpath}">${video.name}</a><br> 视频标签:${video.label } <br> 视频描述:${video.description } <br> 点击量:${video.count } <br> 作者:${video.userName} <br> 上传时间:${video.createTime} <br><br> </c:forEach> <div> <jsp:include page="fenye.jsp"> <jsp:param name="url4page" value="videos.jsp" /> </jsp:include> </div> <jsp:include flush="true" page="foot.jsp"></jsp:include> </body> </html> 4. 数据库中查询数据: public static List listAllVideos(int startRow,int size) { Connection conn=null; PreparedStatement ps=null; String sql = "select video.id,vpath,name,label,count,description,userName,video.createTime from video join user on user.id=video.userId order by createTime desc limit ?,? "; ResultSet rs = null; List list = new ArrayList(); try { conn= DBConn.getConnection(); ps = conn.prepareStatement(sql); ps.setInt(1, startRow); ps.setInt(2, size); rs = ps.executeQuery(); while(rs.next()){ com.puckasoft.video.vo.Video video = new com.puckasoft.video.vo.Video(rs.getInt("id"),rs.getString("vpath"), rs.getString("name"),rs.getString("label"),rs.getString("description"), rs.getString("userName"),rs.getInt("count"),rs.getTimestamp("createTime")); list.add(video); } } catch (SQLException e) { e.printStackTrace(); }finally { DBConn.close(ps, conn); } return list; } public static int countAllVideos(){ Connection conn=null; PreparedStatement ps=null; String sql = "select count(*) from video"; ResultSet rs = null; int count=0; try { conn= DBConn.getConnection(); ps = conn.prepareStatement(sql); rs = ps.executeQuery(); while(rs.next()){ count=rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally { DBConn.close(ps, conn); } return count; }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服