急求一个jsp+ajax实现分页的例子

 我来答
残缺_天逆
2011-10-02 · TA获得超过107个赞
知道小有建树答主
回答量:99
采纳率:0%
帮助的人:101万
展开全部
分页实现步骤:
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="http://java.sun.com/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 %>${page.last}">末页</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.puckasoft.video.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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 = 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;
}
追问
这是Ajax实现的?开玩笑
追答
尴尬,貌似贴错了。就当借鉴一下吧,我们学的是javascript、jsp和servlet。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式