分页现实的步骤:
确定每页显示的数据数量
计算显示的页数
编写SQL语句
<%@page import="com.jereh.entity.News"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="com.jereh.dao.BaseDao"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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%>">
<title>分页</title>
<link href="CSS/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
String curPageStr= request.getParameter("curPage");
int pageSize =3; //页面大小
int curPage = 1; //当前页码
int totalPages = 0;//总页面
int totalCount = 0;//总记录数
int prePage=1; //上页页码
int nextPage =1; //下页页码
//当前页面
if(curPageStr !=null){
curPage = Integer.valueOf(curPageStr);
}
//求出符合条件的总记录数
BaseDao baseDao = new BaseDao();
String queryTotalCount="select count(1) from news_info";
ResultSet rs = baseDao.executeQuery(queryTotalCount);
if(rs.next()){
totalCount = rs.getInt(1);
}
baseDao.closeAll();
//计算总页数
totalPages = totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;
//查询符合条件的记录
String query="select * from (select A.*, rownum rn "
+ " from (select * from news_info order by pub_date desc) A) B "
+" where rn between ? and ?";
int start=(curPage-1)*pageSize+1; //当前页面开始记录
int end =curPage*pageSize; //当前页面结束记录
List<Object> params = new ArrayList<Object>();
params.add(start);
params.add(end);
List<News> newList = new ArrayList<News>();
ResultSet newsRs = baseDao.executeQuery(query, params);
while(newsRs.next()){
News news = new News();
news.setId(newsRs.getInt("ID"));
news.setUser(newsRs.getString("ADD_USER"));
news.setCategoryId(newsRs.getInt("CATEGORY_ID"));
news.setTitle(newsRs.getString("TITLE"));
news.setSummary(newsRs.getString("SUMMARY"));
news.setContent(newsRs.getString("CONTENT"));
news.setImgPath(newsRs.getString("IMG_PATH"));
news.setPubDate(newsRs.getString("PUB_DATE"));
news.setShow(newsRs.getBoolean("IS_SHOW"));
newList.add(news);
}
baseDao.closeAll();
if(curPage-1<=1){
prePage =1;
}else{
prePage=curPage-1;
}
if(curPage+1>totalPages){
nextPage=totalPages;
}else{
nextPage = curPage+1;
}
%>
<ul class="classlist">
<%
for(News n : newList){
%>
<li><a href="news_read.html?nid=165"> <%=n.getTitle() %> </a>
<span> <%=n.getPubDate() %></span></li>
<%} %>
<p align="right"> 当前页数:[<%=curPage %>/<%=totalPages %>]
<a href="ch06/ch06_1.jsp?curPage=<%=prePage%>">上一页 </a>
<a href="ch06/ch06_1.jsp?curPage=<%=nextPage%>">下一页 </a>
<a href="ch06/ch06_1.jsp?curPage=<%=totalPages%>">末页</a>
</p>
</ul>
</body>
</html>
|
|