创建一个分页对象PageBean<T>来存储分页信息+实体信息,
客户端请求时传递分页信息,
服务端将实体信息+分页信息放进分页对象返回给客户端。
实例如下:
listStudent.jsp
<%@page import="com.pojo.Student"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <style type="text/css"> </style> </head> <script type="text/javascript"> function del(id){ if(confirm("是否确定要删除该数据?")){ window.location.href="updateStu?type=del&id="+id; } } function toPage(index){ window.location.href="queryStu?index="+index; } </script> <body> <div align="center"> <table style=" 500px;" border="1"> <tr> <th>编号</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>生日</th> <th>专业</th> <th>操作</th> </tr> <c:forEach items="${pb.list}" var="stu"> <tr> <td>${stu.stuId}</td> <td>${stu.stuName }</td> <td>${stu.stuAge}</td> <td> <c:if test="${stu.stuSex=='1' }">男</c:if> <c:if test="${stu.stuSex=='2' }">女</c:if> </td> <td><fmt:formatDate value="${stu.stuDate }" pattern="yyyy-MM-dd"/></td> <td>${stu.showStuProfess}</td> <td><a href="javascript:del('${stu.stuId }')">删除</a> <a href="updateStu?type=toupdate&id=${stu.stuId }">修改</a></td> </tr> </c:forEach> <tr> <td colspan="7" align="center"> <a href="queryStu?index=1">首页</a> <a <c:if test="${pb.pageIndex>1 }"> href="queryStu?index=${pb.pageIndex-1 }"</c:if>>上一页</a> <a <c:if test="${pb.pageIndex<pb.maxPage }"> href="queryStu?index=${pb.pageIndex+1 }"</c:if>>下一页</a> <a href="queryStu?index=${pb.maxPage }">末页</a> 跳转到<select onchange="toPage(this.value)"> <c:forEach begin="1" end="${pb.maxPage }" varStatus="status"> <option <c:if test="${pb.pageIndex==status.index}">selected="selected"</c:if> >${status.index}</option> </c:forEach> </select> 总共${pb.maxPage }页 </td> </tr> </table> </div> </body> </html>
PageBean.java
package com.pojo; /** * @author allen * 分页对象 * @param <T> 表示该分页对象可以针对任何数据 */ import java.util.List; public class PageBean<T> { int pageIndex;//当前第几页 int pageCount;//每页显示的条数 int totalCount;//数据的总量 int maxPage;//最大页数 List<T> list; public int getPageIndex() { return pageIndex; } public void setPageIndex(int pageIndex) { this.pageIndex = pageIndex; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getMaxPage() { return maxPage; } public void setMaxPage(int maxPage) { this.maxPage = maxPage; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }
StudentService.java
package com.service; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.List; import com.dao.IStudentDAO; import com.dao.StudentDAO; import com.pojo.PageBean; import com.pojo.Student; public class StudentService implements IStudentService { IStudentDAO stuDAO = new StudentDAO(); @Override public int addStu(String stuname, String stusex, String stuage, String studate, String stuprofess) { Student stu = new Student(); stu.setStuName(stuname); stu.setStuSex(stusex); stu.setStuAge(Integer.valueOf(stuage)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { stu.setStuDate(sdf.parse(studate)); } catch (ParseException e) { e.printStackTrace(); } stu.setStuProfess(stuprofess); return stuDAO.saveStu(stu); } @Override public List<Student> getAllStu() { return stuDAO.queryStu("select * from student"); } @Override public void delStu(String id) { stuDAO.delStu(id); } @Override public Student getStuById(String id) { List<Student> list = stuDAO.queryStu("select * from student where stuid='" + id + "'"); if (list != null && list.size() > 0) { return list.get(0); } return null; } @Override public void updateStu(String stuid, String stuname, String stusex, String stuage, String studate, String stuprofess) { Student stu = new Student(); stu.setStuId(Integer.valueOf(stuid)); stu.setStuName(stuname); stu.setStuSex(stusex); stu.setStuAge(Integer.valueOf(stuage)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { stu.setStuDate(sdf.parse(studate)); } catch (ParseException e) { e.printStackTrace(); } stu.setStuProfess(stuprofess); stuDAO.updateStu(stu); } @Override public PageBean<Student> queryStudentPageByIndex(int index, int pageCount, String sql) { PageBean<Student> pb = new PageBean<>(); pb.setPageIndex(index); pb.setPageCount(pageCount); pb.setTotalCount(stuDAO.getCountBySql(sql)); int maxPage = pb.getTotalCount() % pageCount == 0 ? pb.getTotalCount() / pageCount : (pb.getTotalCount() / pageCount) + 1; pb.setMaxPage(maxPage); sql = "select * from (select a.*,rownum num_ from ("+sql+") a) b where b.num_>="+((index-1)*pageCount+1)+" and b.num_<="+index*pageCount; pb.setList(stuDAO.queryStu(sql)); return pb; } }
QueryStudentServlet.java
package com.control; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.pojo.PageBean; import com.pojo.Student; import com.service.IStudentService; import com.service.StudentService; @WebServlet("/queryStu") public class QueryStudentServlet extends HttpServlet{ IStudentService stuSer = new StudentService(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String pageIndex = req.getParameter("index"); int index = 0; if(pageIndex==null){ index = 1; }else{ index = Integer.valueOf(pageIndex); } //1.调用业务查询数据 分页显示 PageBean<Student> pb = stuSer.queryStudentPageByIndex(index, 5, "select * from student"); //2.将数据保存到request中传递到页面 req.setAttribute("pb", pb); //3.转发页面 req.getRequestDispatcher("student/listStudent.jsp").forward(req, resp); } }