栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java(基于Struts2) 分页实现代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java(基于Struts2) 分页实现代码

分页实现的基本过程是这样的:

1. 设置自己的分页器的基本参数(可以从配置文件中读取)

  ■每页显示的记录条数

  ■每次最多显示多少页

2. 编写设置分页器其他参数的函数

    主要参数有以下几个:

    总记录条数

    总页数

    当前页号:现在显示的页数

    每页显示的记录条数

    当前页开始行(第一行是0行)

    第一页页号

    最后页页号

    下一页页号

    上一页页号

    画面上显示的起始页号

    画面上显示的结束页号

    参数基本实现原理:设置以上各个参数,实际上只需要三个参数就可以对所有的其他变量进行设置,即总记录条数,每页显示记录数,每次最多显示多少页。

    分页器的代码实现如下(省略get,set函数):

    Page.java

复制代码 代码如下:
{
        this.onePageSize = Integer.valueOf(PageResource.get(PageResource.ONE_PAGE_SIZE));
        this.displayPageCount = Integer.valueOf(PageResource.get(PageResource.DISPLAY_PAGE_COUNT)) - 1;
    }

   
    private int displayPageCount;

   
    private int onePageSize;

   
    private int totalRecord;

   
    private int totalPage;

   
    private int currentPageNum = 1;

   
    private int currentStartRow;

   
    private int firstPageNum = 1;

   
    private int lastPageNum;

   
    private int nextPageNum;

   
    private int prevPageNum;

   
    private int startPageNum;

   
    private int endPageNum;

   
    public Page(int totalRecord) {
        this.totalRecord = totalRecord;
        this.setPageInfo();
    }

    public Page() {
    }

    public void setPageInfo() {
        this.totalPage = (totalRecord + onePageSize - 1) / onePageSize;
        this.currentPageNum = Math.max(1, Math.min(currentPageNum, totalPage));

        this.lastPageNum = this.totalPage;
        this.nextPageNum = Math.min(this.totalPage, this.currentPageNum + 1);
        this.prevPageNum = Math.max(1, this.currentPageNum - 1);

        // 分页控制信息
        this.currentStartRow = (this.currentPageNum - 1) * onePageSize;

        startPageNum = Math.max(this.currentPageNum - displayPageCount / 2,
                firstPageNum);
        endPageNum = Math.min(startPageNum + displayPageCount, lastPageNum);
        if (endPageNum - startPageNum < displayPageCount) {
            startPageNum = Math.max(endPageNum - displayPageCount, 1);
        }
    }

3. 编写前端代码(以Struts2为例)

当在前台点击各个跳转页面的链接时,只需要将要跳转到的页号和总页数传给后台,后台会重新更新分页器,进而实现页码的跳转。

复制代码 代码如下:

           
                总页数:
               
                总记录数:
               
           
           
               
                   
               

               
                   
               

           

            首页

           
               
                   
               

               
                   
               

           

            上一页

           
               
               
               
                   
                       
                   

                   
                       
                           
                               
                           

                           
                               
                           

                       

                       
                   

               

           

           
               
                   
               

               
                   
               

           

            下一页

        
               
                   
               

               
                   
               

           

         尾页
       

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152957.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号