|
|
发表于:
2013-6-19 11:15:37 |
[全部帖]
[楼主帖]
|
楼主
|
|
|
|
Java代码 - 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 + "]";
- }
-
- }
Java代码
- 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();
- }
- }
Java代码
- 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);
- }
-
- }
Java代码
Java代码
- 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;
- }
-
- }
Java代码
Html代码
- <%@ 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>
运行结果图:
|
|
|