目录
前言.为什么需要分页?
在一个网页中,如果一个网站的数据太多太长会让用户很难有耐心的看下去,因此,就有了分页。此外,分页还能增加网页的美观度。
1.分页的属性:
- 总数据量 rows;
- 页面的数据多少条 pageSize;
- 总的页面数 pageCount;
- 最后一页 end;
- 第一页 start;
- 当前页 pageIndex;
2.变量的设计:
每页开始的条数:start = (pageIndex - 1) * pageSize + 1;
每页结束的条数:end = pageIndex * pageSize;
3.查询语句的设计(主要用的是Oracle):
select * from (select a.*,rownum myid from 表名) b where myid between ? and ?;
Oracle一般使用伪列来分页;
sqlServer,mySql中一般使用top来分页;
核心代码:
表的创建
--1.新闻信息表
create table tb_news(
nid number(7) primary key,--新闻编号
sid number(7),--主题编号
title varchar2(100),--标题
author varchar2(50),--作者
summary varchar2(500),--摘要
content varchar2(999),--内容
filename varchar2(999),--图片名
addtime varchar2(200)--增加时间
)
dao包的设计:
/**
* 分页的方法:每页最多展示5条数据
* @param pageIndex 当前页码
* @return
* @throws SQLException
*/public List<News> query(int pageIndex) throws SQLException{
int pageSize = 5;//页大小
int start = (pageIndex - 1) * pageSize+1;//每页开始数据条数
int end = pageIndex * pageSize;//每页结束的数据条数
listNews = new ArrayList<News>();
con = DBhelper.getCon();
sql = "select nid,sid,title,author,summary,content,filename,addtime from (select a.*,rownum myid from tb_news a ) b where myid between ? and ?";
ps = con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, start);
ps.setInt(2, end);
rs = ps.executeQuery();
while(rs.next()) {
news = new News(rs.getInt("nid"),rs.getInt("sid"),rs.getString("title"),rs.getString("author"),
rs.getString("summary"), rs.getString("content"), rs.getString("filename"), null);
listNews.add(news);
}
DBhelper.closeObj(con, ps, rs);
return listNews;
}
/**
* 求新闻信息表里面可展示的总页码
* @return
* @throws SQLException
*/
public int getMaxPage(String str) throws SQLException{
con = DBhelper.getCon();
sql = "select count(0) from tb_news where sid like '%"+str+"%'";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
if(rs.next()) {
maxPage = rs.getInt(1) / 5;
if(rs.getInt(1) % 5 != 0) {
maxPage ++;
}
}
DBhelper.closeObj(con, ps, rs);
return maxPage;
}
主界面的设计:
<%@page import="com.zking.entity.News"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.dao.NewsDao"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加主题--管理后台</title>
<link href="/jsp_05/static/css/admin.css" rel="stylesheet" type="text/css" />
<style type="text/css">
a{
text-decoration: none;
}
</style>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");NewsDao nd = new NewsDao();
//获取request作用域里面的listNews的数据
List<News> listNews = (List)request.getAttribute("listNews");
//作用域里面保存的当前页码变量
int pageIndex = Integer.parseInt(request.getAttribute("pageIndex").toString());
%>
<div id="header">
<div id="welcome">欢迎使用新闻管理系统!</div>
<div id="nav">
<div id="logo"><img src="/jsp_05/static/images/logo.jpg" alt="新闻中国" /></div>
<div id="a_b01"><img src="/jsp_05/static/images/a_b01.gif" alt="" /></div>
</div>
</div>
<div id="admin_bar">
<div id="status">管理员: <%= session.getAttribute("uname") %>      <a href="<%=request.getContextPath() %>/admin/loginOut.jsp">注销</a></div>
<div id="channel"> </div>
</div>
<div id="main">
<div id="opt_list">
<ul>
<li><a href="<%=request.getContextPath()%>/admin/newsAdd.jsp">添加新闻</a></li>
<li><a href="#">编辑新闻</a></li>
<li><a href="#">查找新闻</a></li>
<li><a href="#">添加主题</a></li>
<li><a href="#">编辑主题</a></li>
</ul>
</div>
<div id="opt_area">
<form action="<%=request.getContextPath()%>/loadDataServlet.do" method = "post">
<input type = "text" name = "str" placeholder = "请输入新闻标题关键字"/>
<button>提交</button>
</form>
<ul class="classlist">
<%
for(News n : listNews){
%>
<li> <a href = "<%=request.getContextPath()%>/admin/newsRead.jsp?uname=<%=request.getParameter("uname")%>&nid=<%=n.getNid()%>"><%=n.getTitle()%>:</a>
<%= n.getSummary() %>
<span>
作者:<%= n.getAuthor() %>
    
<a href='<%=request.getContextPath()%>/admin/newsUpdate.jsp?nid=<%=n.getNid()%>'>修改</a>
    
<a href='<%=request.getContextPath()%>/newsDelServlet.do?nid=<%=n.getNid()%>' οnclick='return confirm("删除请点击确认!")'>删除</a>
</span>
</li>
<%
}
%>
<li class='space'></li>
<!-- 当点击翻页按钮的时候,跳转到加载数据的loadDataServlet里面,并且把页面变量传过去 -->
<p align="center">
<a href="<%=request.getContextPath()%>/loadDataServlet.do?pageIndex=1">首页</a>
<a href="<%=request.getContextPath()%>/loadDataServlet.do?pageIndex=<%=pageIndex>1?pageIndex-1:1 %>">上一页</a>
当前页数:[<%=pageIndex %>/<%=maxPage %>]
<a href="<%=request.getContextPath()%>/loadDataServlet.do?pageIndex=<%=pageIndex<maxPage?pageIndex+1:maxPage %>">下一页</a>
<a href="<%=request.getContextPath()%>/loadDataServlet.do?pageIndex=<%=maxPage %>">末页</a>
</p>
</ul>
</div>
</div>
<div id="site_link"> <a href="#">关于我们</a><span>|</span> <a href="#">Aboue Us</a><span>|</span> <a href="#">联系我们</a><span>|</span> <a href="#">广告服务</a><span>|</span> <a href="#">供稿服务</a><span>|</span> <a href="#">法律声明</a><span>|</span> <a href="#">招聘信息</a><span>|</span> <a href="#">网站地图</a><span>|</span> <a href="#">留言反馈</a> </div>
<div id="footer">
<p class="">24小时客户服务热线:010-68988888      <a href="#">常见问题解答</a>      新闻热线:010-627488888<br />
文明办网文明上网举报电话:010-627488888      举报邮箱:<a href="#">jubao@jb-aptech.com.cn</a></p>
<p class="copyright">Copyright © 1999-2009 News China gov, All Right Reserver<br />;
新闻中国 版权所有</p>
</div>
</body>
</html>
servlet包的设计:
package com.zking.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import com.zking.dao.NewsDao;
import com.zking.entity.News;public class LoadDataServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
//1.设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset = utf-8");
NewsDao nd = new NewsDao();
//定义当前页面变量
int pageIndex = 1;//默认展示第一页的数据
//获取前台传来的页码量
String mypageIndex = req.getParameter("pageIndex");
//把从前台获取的页码mypageIndexreq赋给当前页码pageIndex
if(null != mypageIndex) {
pageIndex = Integer.parseInt(mypageIndex);
}
//2.获取out内置对象
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
List<News> listNews = nd.query(pageIndex);//调用分页的方法
//把获取的数据集合listNews保存到request作用域里面
req.setAttribute("listNews", listNews);
//把当前页码pageIndex保存到request作用域里面
req.setAttribute("pageIndex", pageIndex);
//通过转发跳转到admin页面
req.getRequestDispatcher("/admin/admin.jsp").forward(req, resp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
最终效果展示图: