本内容使用的另一种方式实现分页功能,以前我们做分页都是在Controller的方法上传pageNum和pageSize这2个值,要么以形参方式(get请求),要么以实体方式(post请求),
现在我们可以使用ThreadLocal是一个线程内部的存储类,可以在指定线程内存储数据,数据存储以后,只有指定线程可以得到存储数据,这样就不需要我们在方法上传值了。
1、创建本地分页线程实体类
package com.zc.bean; public class PaginationContext { // 定义两个threadLocal变量:pageNum和pageSize private static ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>(); // 保存第几页 private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>(); // 保存每页记录条数 /** * pageNum :get、set、remove */ public static int getPageNum() { Integer pn = pageNum.get(); if (pn == null) { return 0; } return pn; } public static void setPageNum(int pageNumValue) { pageNum.set(pageNumValue); } public static void removePageNum() { pageNum.remove(); } /** * pageSize :get、set、remove */ public static int getPageSize() { Integer ps = pageSize.get(); if (ps == null) { return 0; } return ps; } public static void setPageSize(int pageSizeValue) { pageSize.set(pageSizeValue); } public static void removePageSize() { pageSize.remove(); } }
2、创建分页拦截器
package com.zc.app.handler; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.zc.bean.PaginationContext; import com.zc.common.utils.StringUtils; public class PageFilter implements Filter { private static final Logger logger=LoggerFactory.getLogger(PageFilter.class); private String[] prefixIgnores; public PageFilter() { } @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; if (isIgnore(httpRequest.getRequestURI())) {//放行 chain.doFilter(request, response); }else{ PaginationContext.setPageNum(getPageNum(httpRequest)); PaginationContext.setPageSize(getPageSize(httpRequest)); try { chain.doFilter(request, response); } // 使用完Threadlocal,将其删除。使用finally确保一定将其删除 finally { PaginationContext.removePageNum(); PaginationContext.removePageSize(); } } } /** * 获得pager.offset参数的值 * * @param request * @return */ protected int getPageNum(HttpServletRequest request) { int pageNum = 1; try { String pageNums = request.getParameter("pageNum"); if (pageNums != null && StringUtils.isNumeric(pageNums)) { pageNum = Integer.parseInt(pageNums); } } catch (NumberFormatException e) { logger.error(e.getMessage(), e); } return pageNum; } /** * 设置默认每页大小 * * @param request * @return */ protected int getPageSize(HttpServletRequest request) { int pageSize = 10; // 默认每页10条记录 try { String pageSizes = request.getParameter("pageSize"); if (pageSizes != null && !("".equals(pageSizes)) && StringUtils.isNumeric(pageSizes)) { pageSize = Integer.parseInt(pageSizes); } } catch (NumberFormatException e) { logger.error(e.getMessage(), e); } return pageSize; } @Override public void init(FilterConfig config) throws ServletException { String contextPath=config.getServletContext().getContextPath(); String ignores=config.getInitParameter("prefixIgnores"); if (null!=ignores && !("".equals(ignores))) { String[] ignoresArray=ignores.split(","); prefixIgnores=new String[ignoresArray.length]; for (int i = 0; i < ignoresArray.length; i++) { prefixIgnores[i]=contextPath+ignoresArray[i]; } } } /** * @description 判断uri是否执行过滤 * @param uri * @return true=不过滤 */ private boolean isIgnore(String uri){ boolean flag=false; if (null!=prefixIgnores && null!=uri) { for (int i = 0; i < prefixIgnores.length; i++) { if (uri.startsWith(prefixIgnores[i])) { flag=true; } } } return flag; } }
3、使用
PageHelper.startPage(PaginationContext.getPageNum(), PaginationContext.getPageSize());
PageList<Company> fileList = companyMapper.getPageList(companyDTO);