Chinaunix首页 | 论坛 | 博客
  • 博客访问: 742392
  • 博文数量: 225
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2722
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-03 17:32
文章分类

全部博文(225)

文章存档

2019年(7)

2018年(16)

2017年(1)

2016年(26)

2015年(41)

2014年(15)

2013年(119)

我的朋友

分类: Java

2013-04-11 14:30:03

1.pager.java类


点击(此处)折叠或打开

  1. import java.util.ArrayList;
  2. import java.util.List;

  3. public class Pager {
  4.     private int dataSize = 0;
  5.     private String pageUrl = "";// 访问每个分页的基准链接
  6.     private int totalRows = 0;// 记录总数

  7.     private int totalPages = 0; // 总页数

  8.     private int pageSize = 10;

  9.     // 每页显示数据条数,默认为10条记录
  10.     private int currentPage = 1; // 当前页数

  11.     private boolean hasPrevious = false; // 是否有上一页

  12.     private boolean hasNext = false; // 是否有下一页

  13.     private boolean hasFirst = false; // 是否显示第一页

  14.     private boolean hasLast = false; // 是否显示最后一页
  15.     private boolean hasCurrent = true;// 是否显示当前页面

  16.     // ? 第一页 ... < 上一页 2 3 4 5 6 下一页 > ... 最后页 ?
  17.     private int navigationCount = 2;// 导航条上显示的最多的分页的个数为navigationCount*2+1 个

  18.     private List data;

  19.     private List<String> prePages = new ArrayList<String>();

  20.     private List<String> postPages = new ArrayList<String>();
  21.     
  22.     private String isThisCollege="0";
  23.     
  24.     private List<Object[]> thirdMenu=new ArrayList<Object[]>();

  25.     /**
  26.      *
  27.      */

  28.     public Pager() {
  29.     }

  30.     /**
  31.      * Initialize Pager
  32.      *
  33.      * @param totalRows
  34.      * total record rows
  35.      * @param pageSize
  36.      * total record is hold by every page
  37.      */
  38.     public void init(int totalRows, int pageSize) {
  39.         this.totalRows = totalRows;
  40.         this.pageSize = pageSize;
  41.         totalPages = ((totalRows + pageSize) - 1) / pageSize;
  42.         refresh(); // 刷新当前页面信息
  43.     }

  44.     public void init(int totalRows, int curpage, int pageSize) {
  45.         this.totalRows = totalRows;
  46.         this.pageSize = pageSize;
  47.         this.currentPage = curpage;
  48.         this.totalPages = ((totalRows + pageSize) - 1) / pageSize;
  49.         refresh(); // 刷新当前页面信息
  50.     }

  51.     /**
  52.      * @return Returns the currentPage.
  53.      */
  54.     public int getCurrentPage() {
  55.         return currentPage;
  56.     }

  57.     /**
  58.      * @param currentPage
  59.      * current page
  60.      */
  61.     public void setCurrentPage(int currentPage) {
  62.         this.currentPage = currentPage;
  63.         refresh();
  64.     }

  65.     /**
  66.      * @return Returns the pageSize.
  67.      */
  68.     public int getPageSize() {
  69.         return pageSize;
  70.     }

  71.     /**
  72.      * @param pageSize
  73.      * The pageSize to set.
  74.      */
  75.     public void setPageSize(int pageSize) {
  76.         this.pageSize = pageSize;
  77.         refresh();
  78.     }

  79.     /**
  80.      * @return Returns the totalPages.
  81.      */
  82.     public int getTotalPages() {
  83.         return totalPages;
  84.     }

  85.     /**
  86.      * @param totalPages
  87.      * The totalPages to set.
  88.      */
  89.     public void setTotalPages(int totalPages) {
  90.         this.totalPages = totalPages;
  91.         refresh();
  92.     }

  93.     /**
  94.      * @return Returns the totalRows.
  95.      */
  96.     public int getTotalRows() {
  97.         return totalRows;
  98.     }

  99.     /**
  100.      * @param totalRows
  101.      * The totalRows to set.
  102.      */
  103.     public void setTotalRows(int totalRows) {
  104.         this.totalRows = totalRows;
  105.         refresh();
  106.     }

  107.     // 跳到第一页
  108.     public void first() {
  109.         currentPage = 1;

  110.         this.setHasPrevious(false);
  111.         refresh();
  112.     }

  113.     // 取得上一页(重新设定当前页面即可)
  114.     public void previous() {
  115.         currentPage--;
  116.         refresh();
  117.     }

  118.     // 取得下一页
  119.     public void next() {

  120.         //System.out.println("next: totalPages: " + totalPages + " currentPage: " + currentPage);

  121.         if (currentPage < totalPages) {
  122.             currentPage++;
  123.         }
  124.         refresh();
  125.     }

  126.     // 跳到最后一页
  127.     public void last() {
  128.         currentPage = totalPages;

  129.         this.setHasNext(false);
  130.         refresh();
  131.     }

  132.     public boolean isHasNext() {
  133.         return hasNext;
  134.     }

  135.     /**
  136.      * @param hasNext
  137.      * The hasNext to set.
  138.      */
  139.     public void setHasNext(boolean hasNext) {
  140.         this.hasNext = hasNext;
  141.     }

  142.     public boolean isHasPrevious() {
  143.         return hasPrevious;
  144.     }

  145.     /**
  146.      * @param hasPrevious
  147.      * The hasPrevious to set.
  148.      */
  149.     public void setHasPrevious(boolean hasPrevious) {
  150.         this.hasPrevious = hasPrevious;
  151.     }

  152.     // 刷新当前页面信息
  153.     public void refresh() {

  154.         if (totalPages <= 1) {
  155.             hasPrevious = false;
  156.             hasNext = false;
  157.             this.hasFirst = false;
  158.             this.hasLast = false;
  159.             this.hasCurrent = false;
  160.             return;
  161.         } else if (currentPage == 1) {
  162.             hasPrevious = false;
  163.             hasNext = true;
  164.         } else if (currentPage == totalPages) {
  165.             hasPrevious = true;
  166.             hasNext = false;
  167.         } else {
  168.             hasPrevious = true;
  169.             hasNext = true;
  170.         }
  171.         if (this.totalPages > this.navigationCount * 2 + 1) {
  172.             if (currentPage - navigationCount > 1) {
  173.                 this.hasFirst = true;
  174.             } else {
  175.                 this.hasFirst = false;
  176.             }
  177.             if (this.currentPage + navigationCount < totalPages) {
  178.                 this.hasLast = true;
  179.             } else {
  180.                 this.hasLast = false;
  181.             }

  182.             this.prePages.clear();
  183.             this.postPages.clear();
  184.             int preCount = navigationCount;
  185.             int postCount = navigationCount;
  186.             if (this.currentPage - this.navigationCount < 1) {
  187.                 preCount = this.currentPage - 1;
  188.                 postCount = this.navigationCount * 2 - preCount;
  189.             } else if (this.currentPage + this.navigationCount > this.totalPages) {
  190.                 postCount = this.totalPages - this.currentPage;
  191.                 preCount = this.navigationCount * 2 - postCount;
  192.             }

  193.             for (int i = preCount; i >= 1; i--) {
  194.                 if (this.currentPage - i > 0) {
  195.                     this.prePages.add(String.valueOf(currentPage - i));
  196.                 }
  197.             }
  198.             for (int j = 1; j <= postCount; j++) {
  199.                 if (this.currentPage + j <= this.totalPages)
  200.                     this.postPages.add(String.valueOf(this.currentPage + j));
  201.             }

  202.         } else {
  203.             this.hasFirst = false;
  204.             this.hasLast = false;
  205.             this.prePages.clear();
  206.             this.postPages.clear();
  207.             for (int i = 1; i < this.currentPage; i++) {
  208.                 this.prePages.add(String.valueOf(i));
  209.             }
  210.             for (int j = this.currentPage + 1; j <= this.totalPages; j++) {
  211.                 this.postPages.add(String.valueOf(j));
  212.             }

  213.         }

  214.     }

  215.     public List getData() {
  216.         return data;
  217.     }

  218.     public void setData(List data) {
  219.         this.data = data;
  220.         this.dataSize = data.size();
  221.     }

  222.     public boolean isHasFirst() {
  223.         return hasFirst;
  224.     }

  225.     public void setHasFirst(boolean hasFirst) {
  226.         this.hasFirst = hasFirst;
  227.     }

  228.     public boolean isHasLast() {
  229.         return hasLast;
  230.     }

  231.     public void setHasLast(boolean hasLast) {
  232.         this.hasLast = hasLast;
  233.     }

  234.     public int getNavigationCount() {
  235.         return navigationCount;
  236.     }

  237.     public void setNavigationCount(int navigationCount) {
  238.         this.navigationCount = navigationCount;
  239.     }

  240.     public List getPostPages() {
  241.         return postPages;
  242.     }

  243.     public void setPostPages(ArrayList<String> postPages) {
  244.         this.postPages = postPages;
  245.     }

  246.     public List getPrePages() {
  247.         return prePages;
  248.     }

  249.     public void setPrePages(ArrayList<String> prePages) {
  250.         this.prePages = prePages;
  251.     }

  252.     public boolean isHasCurrent() {
  253.         return hasCurrent;
  254.     }

  255.     public void setHasCurrent(boolean hasCurrent) {
  256.         this.hasCurrent = hasCurrent;
  257.     }

  258.     public String getPageUrl() {
  259.         return pageUrl;
  260.     }

  261.     public void setPageUrl(String pageUrl) {
  262.         this.pageUrl = pageUrl;
  263.     }

  264.     public int getDataSize() {
  265.         return dataSize;
  266.     }

  267.     public void setDataSize(int dataSize) {
  268.         this.dataSize = dataSize;
  269.     }
  270.     
  271.     public String getIsThisCollege() {
  272.         return isThisCollege;
  273.     }

  274.     public void setIsThisCollege(String isThisCollege) {
  275.         this.isThisCollege = isThisCollege;
  276.     }

  277.     public List<Object[]> getThirdMenu() {
  278.         return thirdMenu;
  279.     }

  280.     public void setThirdMenu(List<Object[]> thirdMenu) {
  281.         this.thirdMenu = thirdMenu;
  282.     }

  283. }

2.action中pager类的用法

Pager pager = new Pager();
int totalRows = service.countTotalRows();//根据条件查询出总共行数
pager.init(totalRows, curpageInt, perpage);
pager.setPageUrl(url);//url是查询条件,url="/subadminPoster.do?act=searchRev&curpage"
List list = service.getData();//根据条件查询list
pager.setData(list);
request.setAttribute("pager", pager);

3.hibernate配合pager类的使用

    public List getDataList(final String hql, final Integer curpage, final Integer perpage)
            throws Exception {
        HibernateTemplate ht = new HibernateTemplate(this.getSessionFactory());
        return (List) ht.execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery(hql);
                query.setFirstResult((curpage - 1) * perpage);
                query.setMaxResults(perpage);
                List result = query.list();
                return result;
            }
        });
    }

4.jsp中对应的显示分页条


    
    
        « 第一页
        ...
    
    
        < 上一页
    
    
        ${looppage}
    
    
        ${pager.currentPage}
    
    
        ${looppage}
    
    
        下一页 >
    
    
        ...
        最后页 »
    

阅读(1915) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~