标签分页

   1. //1. 导入pager-taglib.jar包;  
   2. //2.引入pager-taglib标签:<%@ taglib prefix="pg"  uri="http://jsptags.com/tags/navigation /pager" %>  
   3. //3.建立PagerModel类:  
   4. public class PagerModel {  
   5.     /** 
   6.      * 总记录数 
   7.      */  
   8.     private int total;  
   9.     /** 
  10.      * 当前页结果集 
  11.      */  
  12.     private List datas;  
  13.     public List getDatas() {  
  14.         return datas;  
  15.     }  
  16.     public void setDatas(List datas) {  
  17.         this.datas = datas;  
  18.     }  
  19.     public int getTotal() {  
  20.         return total;  
  21.     }  
  22.     public void setTotal(int total) {  
  23.         this.total = total;  
  24.     }  
  25. }  
  26. //4.pager业务逻辑部分:  
  27. public class AbstractManager extends HibernateDaoSupport {  
  28.     public PagerModel searchHql(String hql){  
  29.         return searchHql(hql,null,SystemContext.getOffset(),SystemContext.getPageSize());  
  30.     }  
  31.     public PagerModel searchHql(String hql,Object datas){  
  32.         return searchHql(hql,new Object[]{datas},SystemContext.getOffset(),SystemContext.getPageSize());  
  33.     }  
  34.     public PagerModel searchHql(String hql,Object[] datas){  
  35.         return searchHql(hql,datas,SystemContext.getOffset(),SystemContext.getPageSize());  
  36.     }  
  37.     public PagerModel searchHql(String hql,int offset,int pageSize){  
  38.         return searchHql(hql,null,offset,pageSize);  
  39.     }  
  40.     public PagerModel searchHql(String hql,Object datas,int offset,int pageSize){  
  41.         return searchHql(hql,new Object[]{datas},offset,pageSize);  
  42.     }  
  43.     public PagerModel searchHql(String hql,Object[] datas,int offset,int pageSize){  
  44.         String countHql = getQuery(hql);  
  45.         Query query = getSession().createQuery(countHql);  
  46.         if(datas !=null && datas.length > 0){  
  47.             for(int i = 0 ; i<datas.length;i++)  
  48.                 query.setParameter(i, datas[i]);  
  49.         }  
  50.         int total = ((Long)query.uniqueResult()).intValue();  
  51.         Query query1=getSession().createQuery(hql);  
  52.         if(datas !=null && datas.length > 0){  
  53.             for(int i = 0 ; i<datas.length;i++)  
  54.                 query1.setParameter(i, datas[i]);  
  55.         }  
  56.         List list =query1.setFirstResult(offset)                                            .setMaxResults(pageSize).list();  
  57.         PagerModel pm = new PagerModel();  
  58.         pm.setDatas(list);  
  59.         pm.setTotal(total);  
  60.         return pm;  
  61.     }  
  62.     public String getQuery(String hql){  
  63.         int index =hql.indexOf("from");  
  64.         if(index != -1){  
  65.             return "select count(*)"+hql.substring(index);  
  66.         }  
  67.         throw new SystemException("查询语句出错!");  
  68.     }  
  69. }  
  70.   
  71. //5.定义offset、pagesize类:  
  72. public class SystemContext {  
  73.     private static ThreadLocal offset = new ThreadLocal();  
  74.     private static ThreadLocal pageSize = new ThreadLocal();  
  75.     public static int getOffset(){  
  76.         Integer os = (Integer)offset.get();  
  77.         if(os == null){  
  78.             return 0;  
  79.         }  
  80.         return os;  
  81.     }  
  82.     public static void setOffset(int offsetvalue){  
  83.         offset.set(offsetvalue);  
  84.     }  
  85.     public static void removeOffset(){  
  86.         offset.remove();  
  87.     }  
  88.     public static int getPageSize(){  
  89.         Integer ps = (Integer)pageSize.get();  
  90.         if(ps == null){  
  91.             return Integer.MAX_VALUE;  
  92.         }  
  93.         return ps;  
  94.     }  
  95.     public static void setPageSize(int pageSizevalue){  
  96.         pageSize.set(pageSizevalue);  
  97.     }  
  98.     public static void removePageSize(){  
  99.         pageSize.remove();  
 100.     }  
 101. }  
 102. //6.过滤器中初始化pagesize、offset:  
 103. public class PagerFilter implements Filter {  
 104.     public void destroy() {  
 105.     }  
 106.     public void doFilter(ServletRequest request, ServletResponse response,  
 107.             FilterChain chain) throws IOException, ServletException {  
 108.         HttpServletRequest httpRequest =(HttpServletRequest)request;  
 109.         SystemContext.setOffset(getOffset(httpRequest));  
 110.         SystemContext.setPageSize(getPageSize(httpRequest));  
 111.         try{  
 112.             chain.doFilter(request, response);  
 113.         }finally{  
 114.             //清空ThreadLocal中的值  
 115.             SystemContext.removeOffset();  
 116.             SystemContext.removePageSize();  
 117.         }             
 118.     }  
 119.     public void init(FilterConfig arg0) throws ServletException {  
 120.     }  
 121.     public int getOffset(HttpServletRequest request){  
 122.         int offset = 0;  
 123.         try {  
 124.             offset = Integer.parseInt(request.getParameter("pager.offset"));  
 125.         } catch (NumberFormatException ignore) {  
 126.         }  
 127.         return offset;  
 128.     }     
 129.         //设置每页显示多少条记录  
 130.     public int getPageSize(HttpServletRequest request){  
 131.         return 3;  
 132.     }  
 133. }  
 134. //7.配置过滤器web.xml中:  
 135. <filter>  
 136.     <filter-name>pager-taglib</filter-name>  
 137.     <filter-class>com.oa.util.filter.PagerFilter</filter-class>  
 138. </filter>  
 139.   
 140. <filter-mapping>  
 141.     <filter-name>pager-taglib</filter-name>  
 142.     <url-pattern>/*</url-pattern>  
 143. </filter-mapping>  
 144. //8.显示效果:  
 145. <pg:pager url="org.do" items="${pm.total }" export="currentPageNumber=pageNumber">  
 146.     <pg:param name="parentId"/>  
 147.     <pg:first>  
 148.         <a href="${pageUrl}" mce_href="${pageUrl}">首页</a>  
 149.     </pg:first>  
 150.     <pg:prev>  
 151.         <a href="${pageUrl }" mce_href="${pageUrl }">前页</a>  
 152.     </pg:prev>  
 153.     <pg:pages>  
 154.         <c:choose>  
 155.             <c:when test="${currentPageNumber eq pageNumber }">  
 156.             <font color="red">${pageNumber }</font>  
 157.             </c:when>  
 158.             <c:otherwise>  
 159.                 <a href="${pageUrl }" mce_href="${pageUrl }">${pageNumber }</a>  
 160.             </c:otherwise>  
 161.         </c:choose>  
 162.     </pg:pages>  
 163.     <pg:next>  
 164.         <a href="${pageUrl }" mce_href="${pageUrl }">后页</a>  
 165.     </pg:next>  
 166.     <pg:last>  
 167.         <a href="${pageUrl }" mce_href="${pageUrl }">尾页</a>  
 168.     </pg:last>  
 169. </pg:pager>  

你可能感兴趣的:(分页)