package com.yh.util;import java.util.ArrayList;import java.util.List;/** * @Description: 分页类 * @Author: 张颖辉(yh) * @CreateDate: 2018/5/7 19:00 * @UpdateUser: 张颖辉(yh) * @UpdateDate: 2018/5/7 19:00 * @UpdateRemark: The modified content * @Version: 1.0 */public class PageInfo{ /*数据*/ private List list; /*总条数*/ private Long count; /*总页数*/ private Integer pages; /*当前页码*/ private Integer currPage; /*每页条数*/ private Integer pageSize; /*开始行*/ private Long startRow; /*显示排列的页码(一些分页控件会使用)*/ private List showNos; /*是否有上一页*/ private Boolean hasPrevious; /*是否有下一页*/ private Boolean hasNext; public PageInfo(Integer currPage, Integer pageSize, Long count) { this.currPage = currPage; this.pageSize = pageSize; this.count = count; this.update(); if (currPage > this.pages) { setCurrPage(this.pages); } } public List getList() { return list; } public void setList(List list) { this.list = list; } public Long getCount() { return count; } public void setCount(Long count) { this.count = count; this.update(); } public Integer getPages() { return pages; } public Integer getCurrPage() { return currPage; } public void setCurrPage(Integer currPage) { currPage = currPage < 1 ? 1 : currPage; this.currPage = currPage; this.update(); } public Integer getPageSize() { return pageSize; } public void setPageSize(Integer pageSize) { if (pageSize < 1) { throw new RuntimeException("pageSize不能小于1"); } this.pageSize = pageSize; this.update(); } public Long getStartRow() { return startRow; } public List getShowNos() { return showNos; } public Boolean getHasPrevious() { this.hasPrevious=this.currPage > 1; return hasPrevious; } public Boolean getHasNext() { this.hasNext=this.currPage < this.pages; return hasNext; } /** * @Description: 更新数据:总页数,和数据开始行数 * @Author: 张颖辉(yh) * @Date: 2018/5/8 10:42 * @param: [] * @return: void * @Version: 1.0 */ private void update() { if (count != null) { if (pageSize != null && pageSize != 0) { if (count % pageSize != 0) { this.pages = (int) (count / pageSize) + 1; } else { this.pages = (int) (count / pageSize); } } } if (currPage != null) { if (pageSize != null) { this.startRow = (currPage - 1) * pageSize.longValue(); } /*显示排列页*/ if (pages != null) { showNos = new ArrayList<>(); for (int i = (currPage - 2 > 0 ? currPage - 2 : 1); i <= (currPage + 2 > pages ? pages : currPage + 2); i++) { showNos.add(i); } } } }}
使用方式:
服务端service关键代码:
Long count=auditLogMapper.getCountByCondition(userId, userName, startTimeStr, endTimeStr);; PageInfopageInfo=new PageInfo(currPage, pageSize, count); List list = auditLogMapper.getAuditsByCondition(userId, userName, startTimeStr, endTimeStr, pageInfo.getStartRow(), pageInfo.getPageSize()); pageInfo.setList(list); return pageInfo;
action/controller中将这个pageInfo放入request.setAttribute()中,以供jsp中使用。
前端关键代码:
引入依赖的js,css
前端使用的是EL表达式结合JSTL标签库,所以要引入:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
分页控件标签:
javascript脚本:
/** * @Description: 分页跳转 * @Author: 张颖辉(yh) * @Date: 2018/5/8 15:17 * @param: * @return: * @Version: 1.0 */ function pageJump(pageNo) { var url = "xxx.action" var paras = ""; if (notNull(pageNo)) { paras = addPara(paras, "pageNo", pageNo); } if (notNull("${userId}")) { paras = addPara(paras, "userId", "${userId}"); } if (notNull("${userName}")) { paras = addPara(paras, "userName", "${userName}"); } if (notNull("${startTimeStr}")){ paras = addPara(paras, "startTimeStr", "${startTimeStr}"); } if (notNull("${endTimeStr}")){ paras = addPara(paras, "endTimeStr", "${endTimeStr}"); } window.location.href = url + paras; } /** * @Description: 添加参数 * @Author: 张颖辉(yh) * @Date: 2018/5/8 15:16 * @param: * @return: * @Version: 1.0 */ function addPara(paras, paraName, paraValue) { if (paras.length == 0) { paras = "?" + paraName + "=" + paraValue } else { paras += "&" + paraName + "=" + paraValue; } return paras; } /** * @Description: 查空 * @Author: 张颖辉(yh) * @Date: 2018/5/8 15:16 * @param: * @return: * @Version: 1.0 */ function notNull(x) { if (x !== null && x !== undefined && x !== '') { return true; } else { return false; } }
显示效果: