分页工具

来源:互联网 发布:通过网络走群众路线 编辑:程序博客网 时间:2024/06/14 09:23

一、分页工具类


package com.ygr.usermanage.utils;public class PageUtil {/**每页多少条数据*/private int pageSize = 10;/**总记录条数*/private int totalCount;   /**当前页*/private int currentPage;/**总页数*/private int pageCount;public PageUtil(int pageSize, int totalCount, int currentPage) {this.pageSize = pageSize;this.totalCount = totalCount;this.setCurrentPage(currentPage);}/** * 当前页边界检查 * 不大不大于总页数,最小不小于一页 * @param currentPage */public void setCurrentPage(int currentPage) {int activePage = currentPage <= 0 ? 1 : currentPage;activePage = activePage > getPageCount() ? getPageCount() : activePage;this.currentPage = activePage;}/** * 计算总页数 * @return */public int getPageCount() {pageCount = totalCount / pageSize;  // 26  /  10     2   int mod = totalCount % pageSize;    // 26  %  10     6   if (mod != 0) {pageCount++;   //3}return totalCount == 0 ? 1 : pageCount;}/** *  获取开始记录序号 *   *  每页显示10条记录 *  1页   0    0-9   *  2页   10   10-19       *  3页   20    20-29 *    ..... *  n页    (n-1)*10 *     */public int getFromIndex() {return (currentPage - 1) * pageSize;}/** * 结束记录序号 * @return */public int getToIndex() {return Math.min(totalCount, currentPage * pageSize);}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}}


二、持久层中的方法:

@Overridepublic List<Car> getPageUserLists(int getFromIndex,int getPageSize) {List<Car> lists = getHibernateTemplate().execute(new HibernateCallback<List<Car>>() {@Overridepublic List<Car> doInHibernate(Session session) throws HibernateException {Query query = session.createQuery("FROM Car ").setFirstResult(getFromIndex).setMaxResults(getPageSize);List<Car> lists = (List<Car>) query.list();return lists;}});return lists;// 重点在于两个方法setFirstResult()和setMaxResults()查询数据库中位于这个两个id坐标之间的数据返回得到的一个数组,即为每个页面中显示的数据// setFirstResult()中的参数为,获取每个分页第一个数据的坐标位置,// setMaxResults()中的参数为每页显示多少条记录个数}

三、控制层中的代码:


@RequestMapping("/show")public String showAll(Integer pageNum, Map<String, Object> map, Model model) {int pageSize = 10 ;// 非空判断页面数,如果页面不为空赋值给currentPageif (pageNum != null && pageNum > 0) {currentPage = pageNum;}int count = carServer.getTotalCount();PageUtil pageUtil = new PageUtil(pageSize, count, currentPage);List<Car> carLists = carServer.getPageUserLists(pageUtil.getFromIndex(), pageUtil.getPageSize());map.put("carLists", carLists);model.addAttribute("currentPage", currentPage);model.addAttribute("totalPage", pageUtil.getPageCount());return "/list_car";}

四、jsp前端界面:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%// 获取请求的上下文String context = request.getContextPath();%><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>汽车列表</title><script type="text/javascript">// 当前第几页数据var currentPage = ${currentPage};// 总页数var totalPage = ${totalPage};function submitForm(actionUrl){window.location.href = actionUrl;}// 第一页function firstPage(){if(currentPage == 1){alert("已经是第一页数据");return false;}else{submitForm("<%=context%>/show.do?pageNum=1");return true;}}// 下一页function nextPage(){if(currentPage == totalPage){alert("已经是最后一页数据");return false;}else{submitForm("<%=context%>/show.do?pageNum=" + (currentPage+1));return true;}}// 上一页function previousPage(){if(currentPage == 1){alert("已经是第一页数据");return false;}else{submitForm("<%=context%>/show.do?pageNum=" + (currentPage-1));return true;}}// 尾页function lastPage(){if(currentPage == totalPage){alert("已经是最后一页数据");return false;}else{submitForm("<%=context%>/show.do?pageNum=${totalPage}");return true;}}</script></head><body><table width="100%" border="0" bgcolor="#cccccc"><tr><td>车牌号</td><td>车辆品牌/型号</td><td>车主姓名</td><td>车主电话</td><td>车主地址</td><td colspan="2">操作</td></tr><%String color = "";int c = 1;%><c:forEach items="${carLists}" var="car"><%if (c == 1) {color = "#ffffff";c = 0;} else {color = "#f5f5f5";c = 1;}%><tr bgcolor="<%=color%>"><td>${car.carNumber}</td><td>${car.carName}</td><td>${car.persenName}</td><td>${car.telephone}</td><td>${car.address}</td><td><a href="./delete.do?id=${car.id}">删除</a></td><td><a href="./update.do?id=${car.id}">修改</a></td></tr></c:forEach></table><br><br>共${totalPage}页  当前第${currentPage}页  <a href="#" onclick="firstPage();">首页</a><a href="#" onclick="previousPage();">上一页</a><a href="#" onclick="nextPage();">下一页</a><a href="#" onclick="lastPage();">尾页</a><br></body></html>



0 0