package cn.pdsu; /** * 类说明:学生信息封装 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-9 下午02:30:50 */ public class Student { private int id; private String name; private String sex; private String resume; public Student(int id, String name, String sex, String resume) { super(); this.id = id; this.name = name; this.sex = sex; this.resume = resume; } public Student() { super(); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getResume() { return resume; } public void setResume(String resume) { this.resume = resume; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", resume=" + resume + "]"; } }
package cn.pdsu; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 类说明:分页控制器 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-9 下午03:47:59 */ public class ControlServlet extends HttpServlet { public ControlServlet() { } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DataSourceDAO dataSourceDAO = new DataSourceDAO(); int countRecord = dataSourceDAO.count(); request.setCharacterEncoding("UTF-8"); // 得到当前页 String currentPage = request.getParameter("page"); if (currentPage == null) currentPage = "1"; int cp = Integer.parseInt(currentPage);// 当前页码 Page<Student> p = new Page<Student>(cp, countRecord); // 数据库中的查询结果 List<Student> list = dataSourceDAO.findStudent(p.getStartIndex(), p.getOnePageCount()); p.setList(list);// 设置结果集 // 设置页码显示类型 PageModel<Student> pageModel = new PageModel<Student>(p, "servlet/ControlServlet", PageModel.NUM_MODEL); request.setAttribute("pageObject", pageModel); getServletContext().getRequestDispatcher("/show.jsp").forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package cn.pdsu; import java.util.LinkedList; import java.util.List; /** * 类说明:数据的封装 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-9 下午02:28:27 */ public class DataSourceDAO { private static List<Student> list = new LinkedList<Student>(); static { for (int i = 0; i < 20; i++) { list.add(new Student(i, "张山" + i, "男", "学生" + i)); } } /** * 模拟数据库中分页查询 * * @param startIndex * 要查询的开始索引 * @param count * 查询的总记录数 * @return 查询出的学生记录 */ public List<Student> findStudent(int startIndex, int count) { int num = count + startIndex; if (num > list.size()) num = list.size(); return list.subList(startIndex, num); } /** * 模拟数据库中查询总记录数 * * @return 总的记录数 */ public int count() { return list.size(); } }
package cn.pdsu; import java.util.List; /** * 类说明:分页方法封装 * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-9 下午02:22:35 */ public class Page<T> { /** * 总页数,通过总记录数和每页显示记录条数计算获得 */ private int countPage; /** * 总记录数 */ private int countRecord; /** * 当前页,默认是第一页 */ private int currentPage = 1; /** * 结果列表 */ private List<T> list = null; /** * 每页显示记录条数 ,默认是每页显示13条记录 */ private int onePageCount = 3; /** * 开始索引,通过当前页和每页显示记录条数计算获得 */ private int startIndex; public Page() { } /** * 两个参数的构造方法,调用该构造方法需要另行设置结果list * * @param currentPage * 当前页 * @param countRecord * 总页数 */ public Page(int currentPage, int countRecord) { this.currentPage = currentPage; this.countRecord = countRecord; calculate(); } /** * 能够设置一页显示多少条记录的构造方法 * * @param currentPage * 当前页 * @param countRecord * 总记录数 * @param onePageCount * 每页最多显示的记录条数 */ public Page(int currentPage, int countRecord, int onePageCount) { super(); this.countRecord = countRecord; this.currentPage = currentPage; this.onePageCount = onePageCount; calculate(); } /** * 计算开始索引和总页数 */ private void calculate() { // 计算开始索引 this.startIndex = (currentPage - 1) * onePageCount; // 计算总页数 this.countPage = (countRecord % onePageCount == 0) ? (countRecord / onePageCount) : (countRecord / onePageCount + 1); } public int getCountPage() { return countPage; } public int getCountRecord() { return countRecord; } public int getCurrentPage() { return currentPage; } public List<T> getList() { return list; } public int getOnePageCount() { return onePageCount; } public int getStartIndex() { return startIndex; } public void setCountPage(int countPage) { this.countPage = countPage; } public void setCountRecord(int countRecord) { this.countRecord = countRecord; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public void setList(List<T> list) { this.list = list; } public void setOnePageCount(int onePageCount) { this.onePageCount = onePageCount; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } }
package cn.pdsu; /** * 开始页,结束页封装 * * @author 作者:user * @version 创建时间:2011-7-19 上午03:08:25 */ public class PageIndex { /** * 结束页 */ private int endIndex; /** * 开始页 */ private int startIndex; /** * 计算开始页和结束页 * * @param viewpagecount * 页面中要显示的页面个数 * @param currentPage * 当前页 * @param totalpage * 总页面数 * @return PageIndex 记录开始页(startindex)和结束页(endindex) */ public static PageIndex getPageIndex(int viewpagecount, int currentPage, int totalpage) { int startpage = currentPage - (viewpagecount % 2 == 0 ? viewpagecount / 2 - 1 : viewpagecount / 2); int endpage = currentPage + viewpagecount / 2; if (startpage < 1) { startpage = 1; if (totalpage >= viewpagecount) endpage = viewpagecount; else endpage = totalpage; } if (endpage > totalpage) { endpage = totalpage; if ((endpage - viewpagecount) > 0) startpage = endpage - viewpagecount + 1; else startpage = 1; } return new PageIndex(startpage, endpage); } public PageIndex(int startIndex, int endIndex) { this.startIndex = startIndex; this.endIndex = endIndex; } public int getEndIndex() { return endIndex; } public void setEndIndex(int endIndex) { this.endIndex = endIndex; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } }
package cn.pdsu; import java.util.List; /** * 类说明:页码显示效果类 。1:TextModel “第一页 上一页 下一页 最后一页”;2:NumModel “第一页 2 3 4 最后一页”; * * @author 作者: LiuJunGuang * @version 创建时间:2011-11-9 下午04:09:22 */ public class PageModel<T> { /** * 文本类型:形如:“第一页 上一页 下一页 最后一页” */ public static final int TEXT_MODEL = 1; /** * 数字类型:形如:NumModel “第一页 2 3 4 最后一页” */ public static final int NUM_MODEL = 2; /** * 页码显示模型 */ private int model = 1; /** * 页码连接URL,不需要添加页码参数 */ private String url; /** * 页码信息的封装 */ private Page page = null; /** * 模型类型的页码 */ private StringBuffer strHtml = null; /** * 数字类型的页码模型中间数字显示个数,例如:第一页 1 2 3 4 5 最后一页,numCount = 5; 默认显示 5个数字 */ private int numCount = 5; /** * 页码的模式默认的文字类型的样式 * * @param page * 页面信息 * @param url * 页面的url地址 */ public PageModel(Page page, String url) { super(); this.url = url; this.page = page; } /** * 页码的模型 * * @param page * 页面信息 * @param url * 页面的url地址 * @param model * 页码的显示样式 */ public PageModel(Page page, String url, int model) { super(); this.model = model; this.url = url; this.page = page; } /** * 页码的模型 * * @param page * 页面信息 * @param url * 页面的url地址 * @param model * 页码的显示样式 * @param numCount * 数字类型的页码,共显示的个数 */ public PageModel(Page page, String url, int model, int numCount) { super(); this.model = model; this.url = url; this.page = page; this.numCount = numCount; } /** * 返回页面的模型 * * @return */ public String getPageModel() { // 组装页码模型 createURL(); return createModel(); } /** * 构建URL */ private void createURL() { url = url.contains("?") ? url + "&page=" : url + "?page="; } /** * 组装页码模型 */ private String createModel() { strHtml = new StringBuffer(); switch (model) { case TEXT_MODEL:// 文本模型 buildTextModel(); break; case NUM_MODEL:// 数字模型 buildNumModel(); break; default:// 文本模型 buildTextModel(); break; } return strHtml.toString(); } /** * 组件数字类型的页码模型 */ private void buildNumModel() { int currentPage = page.getCurrentPage(); int countPage = page.getCountPage(); strHtml.append( "<table width='100%' border='0' cellspacing='0' cellpadding='0'>") .append("<tr><td height='24' align='center'>"); // 构造格式:第一页 1 2 3 4 5 最后一页 PageIndex pageIndex = PageIndex.getPageIndex(numCount, currentPage, countPage); // 不是第一页时,显示首页 if (currentPage > 1) { strHtml.append("<a href='").append(url) .append("1'>首页</a> "); } if (currentPage <= countPage) { for (int i = pageIndex.getStartIndex(); i <= pageIndex .getEndIndex(); i++) { // 当前页加粗 if (currentPage == i) { strHtml.append("<b>").append(i).append("</b> "); } else { strHtml.append("<a href='").append(url).append(i) .append("'>").append(i).append("</a> "); } } // 不是最后一页显示末页 if (currentPage < countPage) { strHtml.append("<a href='").append(url).append(countPage) .append("'>末页</a>"); } } strHtml.append("</td></tr></table>"); } /** * 组件文本类型的页码 */ private void buildTextModel() { int currentPage = page.getCurrentPage(); int countPage = page.getCountPage(); strHtml.append( "<table width='100%' border='0' cellspacing='0' cellpadding='0'>") .append("<tr> <td height='24' align='center'>当前页数:[") .append(currentPage).append("/").append(countPage) .append("] "); if (currentPage > 1) { strHtml.append("<a href='").append(url).append("1'>首页</a>") .append(" <a href='").append(url) .append(currentPage - 1).append("'>上一页</a>"); } if (currentPage < countPage) { strHtml.append(" <a href='").append(url) .append(currentPage + 1) .append("'>下一页</a> <a href='").append(url) .append(countPage).append("'>末页</a>"); } strHtml.append("</td></tr></table>"); } public List<T> getList() { return page.getList(); } }
显示页面:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% 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> </head> <body> <table border="1" width="50%"> <tr><th>ID</th><th>姓名</th><th>性别</th><th>简介</th></tr> <c:forEach items="${ pageObject.list}" var="student" > <tr> <td>${student.id }</td> <td>${student.name }</td> <td>${student.sex }</td> <td>${student.resume }</td> </tr> </c:forEach> </table> ${ pageObject.pageModel} </body> </html>
源码下载:http://aimilin6688.iteye.com/blog/1249234(最下面附件部分)
另外csdn下载地址:http://download.csdn.net/detail/afgasdg/3776861(免资源分)