Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 342|回复: 0

[jsp学习]网上流行的分页类

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-2 04:41:58 | 显示全部楼层 |阅读模式
    流行的分页类:
    1. import java.util.List;
    2. /**
    3. * Helper class that implements paging over a collection.
    4. *
    5. * @author        Simon Brown
    6. */
    7. public class Pageable< T> {
    8.     /** the default page size */
    9.     public static final int DEFAULT_PAGE_SIZE = 10;
    10.     private static final int PAGE_WINDOW = 10;
    11.     /** the list over which this class is paging */
    12.     private List< T> list;
    13.     /** the page size */
    14.     private int pageSize = DEFAULT_PAGE_SIZE;
    15.     /** the current page */
    16.     private int page;
    17.     /** the starting index */
    18.     private int startingIndex;
    19.     /** the ending index */
    20.     private int endingIndex;
    21.     /** the maximum number of pages */
    22.     private int maxPages;
    23.     /**
    24.      * Creates a new instance with the specified list.
    25.      *
    26.      * @param list        a List
    27.      */
    28.     public Pageable(List< T> list) {
    29.         this.list = list;
    30.         this.page = 1;
    31.         this.maxPages = 1;
    32.         calculatePages();
    33.     }
    34.     private void calculatePages() {
    35.         if (pageSize > 0) {
    36.             // calculate how many pages there are
    37.             if (list.size() % pageSize == 0) {
    38.                 maxPages = list.size() / pageSize;
    39.             } else {
    40.                 maxPages = (list.size() / pageSize) + 1;
    41.             }
    42.         }
    43.     }
    44.     /**
    45.      * Gets the list that this instance is paging over.
    46.      *
    47.      * @return    a List
    48.      */
    49.     public List< T> getList() {
    50.         return this.list;
    51.     }
    52.     /**
    53.      * Gets the subset of the list for the current page.
    54.      *
    55.      * @return    a List
    56.      */
    57.     public List< T> getListForPage() {
    58.         return list.subList(startingIndex, endingIndex);
    59.     }
    60.     /**
    61.      * Gets the page size.
    62.      *
    63.      * @return    the page size as an int
    64.      */
    65.     public int getPageSize() {
    66.         return this.pageSize;
    67.     }
    68.     /**
    69.      * Sets the page size.
    70.      *
    71.      * @param pageSize     the page size as an int
    72.      */
    73.     public void setPageSize(int pageSize) {
    74.         this.pageSize = pageSize;
    75.         calculatePages();
    76.     }
    77.     /**
    78.      * Gets the page.
    79.      *
    80.      * @return    the page as an int
    81.      */
    82.     public int getPage() {
    83.         return this.page;
    84.     }
    85.     /**
    86.      * Sets the page size.
    87.      *
    88.      * @param p        the page as an int
    89.      */
    90.     public void setPage(int p) {
    91.         if (p >= maxPages) {
    92.             this.page = maxPages;
    93.         } else if (p <= 1) {
    94.             this.page = 1;
    95.         } else {
    96.             this.page = p;
    97.         }
    98.         // now work out where the sub-list should start and end
    99.         startingIndex = pageSize * (page-1);
    100.         if (startingIndex < 0) {
    101.             startingIndex = 0;
    102.         }
    103.         endingIndex = startingIndex + pageSize;
    104.         if (endingIndex > list.size()) {
    105.             endingIndex = list.size();
    106.         }
    107.     }
    108.     /**
    109.      * Gets the maximum number of pages.
    110.      *
    111.      * @return    the maximum number of pages as an int
    112.      */
    113.     public int getMaxPages() {
    114.         return this.maxPages;
    115.     }
    116.     /**
    117.      * Determines whether there is a previous page and gets the page number.
    118.      *
    119.      * @return    the previous page number, or zero
    120.      */
    121.     public int getPreviousPage() {
    122.         if (page > 1) {
    123.             return page-1;
    124.         } else {
    125.             return 0;
    126.         }
    127.     }
    128.     /**
    129.      * Determines whether there is a next page and gets the page number.
    130.      *
    131.      * @return    the next page number, or 0
    132.      */
    133.     public int getNextPage() {
    134.         if (page < maxPages) {
    135.             return page+1;
    136.         } else {
    137.             return 0;
    138.         }
    139.     }
    140.     /**
    141.      * Gets the minimum page in the window.
    142.      *
    143.      * @return    the page number
    144.      */
    145.     public int getMinPageRange() {
    146.         if (getPage() > PAGE_WINDOW) {
    147.             return getPage() - PAGE_WINDOW;
    148.         } else {
    149.             return 1;
    150.         }
    151.     }
    152.     /**
    153.      * Gets the maximum page in the window.
    154.      *
    155.      * @return    the page number
    156.      */
    157.     public int getMaxPageRange() {
    158.         if (getPage() < (getMaxPages() - PAGE_WINDOW)) {
    159.             return getPage() + PAGE_WINDOW;
    160.         } else {
    161.             return getMaxPages();
    162.         }
    163.     }
    164. }
    复制代码

       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/2/044158562.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-6-3 18:52 , Processed in 0.353008 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表