使用pager-taglib.jar实现的分页技术

//1.导入pager-taglib.jar包; 

 
//2.引入pager-taglib标签:<%@ taglib prefix="pg"  uri="http://jsptags.com/tags/navigation/pager " %>  
//3.建立PagerModel类:  
public class PagerModel {  
     
    private int total;  
     
    private List datas;  
    public List getDatas() {  
        return datas;  
    }  
    public void setDatas(List datas) {  
        this.datas = datas;  
    }  
    public int getTotal() {  
        return total;  
    }  
    public void setTotal(int total) {  
        this.total = total;  
    }  
}  
//4.pager业务逻辑部分:  
public class AbstractManager extends HibernateDaoSupport {  
    public PagerModel searchHql(String hql){  
        return searchHql(hql,null,SystemContext.getOffset(),SystemContext.getPageSize());  
    }  
    public PagerModel searchHql(String hql,Object datas){  
        return searchHql(hql,new Object[]{datas},SystemContext.getOffset(),SystemContext.getPageSize());  
    }  
    public PagerModel searchHql(String hql,Object[] datas){  
        return searchHql(hql,datas,SystemContext.getOffset(),SystemContext.getPageSize());  
    }  
    public PagerModel searchHql(String hql,int offset,int pageSize){  
        return searchHql(hql,null,offset,pageSize);  
    }  
    public PagerModel searchHql(String hql,Object datas,int offset,int pageSize){  
        return searchHql(hql,new Object[]{datas},offset,pageSize);  
    }  
    public PagerModel searchHql(String hql,Object[] datas,int offset,int pageSize){  
        String countHql = getQuery(hql);  
        Query query = getSession().createQuery(countHql);  
        if(datas !=null && datas.length > 0){  
            for(int i = 0 ; i                 query.setParameter(i, datas[i]);  
        }  
        int total = ((Long)query.uniqueResult()).intValue();  
        Query query1=getSession().createQuery(hql);  
        if(datas !=null && datas.length > 0){  
            for(int i = 0 ; i                 query1.setParameter(i, datas[i]);  
        }  
        List list =query1.setFirstResult(offset)                                            .setMaxResults(pageSize).list();  
        PagerModel pm = new PagerModel();  
        pm.setDatas(list);  
        pm.setTotal(total);  
        return pm;  
    }  
    public String getQuery(String hql){  
        int index =hql.indexOf("from");  
        if(index != -1){  
            return "select count(*)"+hql.substring(index);  
        }  
        throw new SystemException("查询语句出错!");  
    }  
}  
 
//5.定义offset、pagesize类:  
public class SystemContext {  
    private static ThreadLocal offset = new ThreadLocal();  
    private static ThreadLocal pageSize = new ThreadLocal();  
    public static int getOffset(){  
        Integer os = (Integer)offset.get();  
        if(os == null){  
            return 0;  
        }  
        return os;  
    }  
    public static void setOffset(int offsetvalue){  
        offset.set(offsetvalue);  
    }  
    public static void removeOffset(){  
        offset.remove();  
    }  
    public static int getPageSize(){  
        Integer ps = (Integer)pageSize.get();  
        if(ps == null){  
            return Integer.MAX_VALUE;  
        }  
        return ps;  
    }  
    public static void setPageSize(int pageSizevalue){  
        pageSize.set(pageSizevalue);  
    }  
    public static void removePageSize(){  
        pageSize.remove();  
    }  
}  
//6.过滤器中初始化pagesize、offset:  
public class PagerFilter implements Filter {  
    public void destroy() {  
    }  
    public void doFilter(ServletRequest request, ServletResponse response,  
            FilterChain chain) throws IOException, ServletException {  
        HttpServletRequest httpRequest =(HttpServletRequest)request;  
        SystemContext.setOffset(getOffset(httpRequest));  
        SystemContext.setPageSize(getPageSize(httpRequest));  
        try{  
            chain.doFilter(request, response);  
        }finally{  
            //清空ThreadLocal中的值  
            SystemContext.removeOffset();  
            SystemContext.removePageSize();  
        }             
    }  
    public void init(FilterConfig arg0) throws ServletException {  
    }  
    public int getOffset(HttpServletRequest request){  
        int offset = 0;  
        try {  
            offset = Integer.parseInt(request.getParameter("pager.offset"));  
        } catch (NumberFormatException ignore) {  
        }  
        return offset;  
    }     
        //设置每页显示多少条记录  
    public int getPageSize(HttpServletRequest request){  
        return 3;  
    }  
}  
//7.配置过滤器web.xml中:  
  
    pager-taglib  
    com.oa.util.filter.PagerFilter  
  
 
  
    pager-taglib  
    /*  
  
//8.显示效果:  
  
      
      
        首页  
   
  
      
        前页  
   
  
      
          
              
            ${pageNumber }  
           
  
              
                ${pageNumber }  
           
  
       
  
   
  
      
        后页  
   
  
      
        尾页  
   
  
 

你可能感兴趣的:(java,web.xml)