分页的实现原理,分页的实现步骤
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
分页的实现原理:
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 ;
st = 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="/jsp/jstl/core"%>
<!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>
<a href="<%=url4page %>${page.prev}">前一页</a>
</c:when>
<c:otherwise>
<b>首页</b>
<b>前一页</b>
</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>
</c:when>
<c:otherwise>
<b>[${i}]</b>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${page.num != page.pageCount}">
<a href="<%=url4page %>${page.next}">后一页</a>
<a href="<%=url4page %>${st}">末页</a>
</c:when>
<c:otherwise>
<b>末页</b>
<b>后一页</b>
</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.puckas oft.video.util.*" %>
<%@ taglib prefix="c" uri="/jsp/jstl/core"%>
<%
String path = request.getContextPath();
String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServ erPort()+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 = 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}">${}</a><br>视频标签:${bel } <br>
视频描述:${video.description } <br>
点击量:${video.count } <br>
作者:${erName} <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=erId 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("descript ion"),
rs.getString("userName"),rs.getInt("count"),rs.getTimestamp("crea teTime"));
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;
}。