当前位置: 移动技术网 > IT编程>开发语言>Java > Java(基于Struts2) 分页实现代码

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

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

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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网