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

时间:2023-11-04 05:58:58 

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

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;
    }

    /** 页号式导航, 最多显示页号数量为displayPageCount+1 */
    private int displayPageCount;

    /** 每页显示的记录条数 */
    private int onePageSize;

    /** 总记录条数 */
    private int totalRecord;

    /** 总页数 */
    private int totalPage;

    /** 当前页号 */
    private int currentPageNum = 1;

    /** 当前页开始行(第一行是0行) */
    private int currentStartRow;

    /** 第一页页号 */
    private int firstPageNum = 1;

    /** 最后页页号 */
    private int lastPageNum;

    /** 下一页页号 */
    private int nextPageNum;

    /** 上一页页号 */
    private int prevPageNum;

    /** 页号式导航 起始页号 */
    private int startPageNum;

    /** 页号式导航 结束页号 */
    private int endPageNum;

    /**
     *
     * @param onePageSize
     * @param currentPageNum
     * @param totalRecord
     */
    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为例)

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


<div>
            <div>
                总页数:
                <s:property value="#request.p.totalPage" />
                总记录数:
                <s:property value="#request.p.totalRecord" />
            </div>
            <s:url id="firstURL" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.firstPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{firstURL}">首页</s:a>

            <s:url id="prev" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.prevPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{prev}">上一页</s:a>

            <s:bean name="org.apache.struts2.util.Counter" id="counter">
                <s:param name="first" value="p.startPageNum" />
                <s:param name="last" value="p.endPageNum" />
                <s:iterator var="pageNum">
                    <s:if test="p.currentPageNum==#pageNum">
                        <s:property />
                    </s:if>
                    <s:else>
                        <s:url id="page" action="PageAction!toPage">
                            <s:param name="p.currentPageNum">
                                <s:property value="#pageNum" />
                            </s:param>
                            <s:param name="p.totalRecord">
                                <s:property value="#request.p.totalRecord" />
                            </s:param>
                        </s:url>
                        <s:a href="%{page}"><s:property /></s:a>
                    </s:else>
                </s:iterator>
            </s:bean>

            <s:url id="next" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.nextPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
            <s:a href="%{next}">下一页</s:a>

         <s:url id="lastURL" action="PageAction!toPage">
                <s:param name="p.currentPageNum">
                    <s:property value="#request.p.lastPageNum" />
                </s:param>
                <s:param name="p.totalRecord">
                    <s:property value="#request.p.totalRecord" />
                </s:param>
            </s:url>
         <s:a href="%{lastURL}">尾页</s:a>
        </div>

标签:Java,Struts2,分页
0
投稿

猜你喜欢

  • springboot如何读取配置文件到静态工具类

    2023-11-28 04:44:54
  • springboot集成JWT实现身份认证(权鉴)的方法步骤

    2023-06-02 12:57:37
  • SpringBoot整合java诊断工具Arthas解读

    2023-08-07 10:39:03
  • C语言数据结构之单链表与双链表的增删改查操作实现

    2023-01-24 07:27:35
  • Java 调用天气Webservice详解及实例代码

    2021-10-09 21:59:03
  • C#调用VB进行简繁转换的方法

    2023-02-25 23:19:43
  • Android 实现沉浸式状态栏的方法

    2023-05-02 21:42:11
  • java开源项目jeecgboot的超详细解析

    2023-07-19 03:30:53
  • 详解WMI RPC 服务器不可用的解决方案

    2023-09-14 14:51:54
  • Springboot下RedisTemplate的两种序列化方式实例详解

    2021-09-11 11:48:47
  • java实现微信扫码登录第三方网站功能(原理和代码)

    2023-08-04 00:44:11
  • 如何使用C语言将数字、字符等数据写入、输出到文本文件中

    2023-09-07 12:09:07
  • 关于C++虚继承的内存模型问题

    2023-04-01 06:10:21
  • c# 调用Win32Api关闭当前应用的方法

    2023-09-22 20:51:26
  • Android 调用系统相机拍摄获取照片的两种方法实现实例

    2022-01-19 21:22:23
  • C# 7.2中结构体性能问题的解决方案

    2022-08-12 23:04:26
  • Android 动画之TranslateAnimation应用详解

    2023-06-27 06:17:59
  • java算法之Math.random()随机概率玩法实例演示

    2023-11-28 23:32:17
  • C语言二叉树的非递归遍历实例分析

    2022-02-08 02:41:15
  • SpringBoot整合Web开发之文件上传与@ControllerAdvice

    2021-09-29 04:43:55
  • asp之家 软件编程 m.aspxhome.com