一、前言

      分页显示这个功能可以说是非常的实用,毫不夸大的说,只要我们涉及到网页,就会实用到分页显示,无论是动态的还是非动态的,效果都是非常实用的。而且不同的语言有不同的效果,下面小编就向大家介绍一下java的真分页实现。

二、说说分页

      在以前小编也实现过使用.NET的分页显示的功能:


【B/S】利用AJAX实现分页



【ASP.NET】Aspnetpager对GridView分页,并导出Excel




【微信平台】艺萌管家APP技术总结(一)——动态分页加载图片


      以上的功能是依托在现有的控件,或者是通过代码完成的。综上小编在学习java的时候发现没有相应的已经封装好的控件,所以只能自己手写,总结一下需要下面的几条:

1. 实例化一个PageBean

     2. 设置每页显示的记录数

     3. 设置每页显示的最大记录数

     4. 设置总记录数,通过查询获得总记录数

     5. 设置总页数

     6. 设置每页显示的数据集合

三、java实现

      用java实现分页的功能,需要我们导入PageBean.java类和PageHibernateCallback.java类:



【SSH】java真分页实现_asp.net



1. 注入page, 获取page在后台

//接收page
    private Integer page;
    public void setPage(Integer page) {
        this.page = page;
    }

2.写分页查询的方法,然后通过PageBean来获得查询得到的对象。

//查询二级分类的方法
    public String findAll(){
        PageBean<CategorySecond> pageBean = adminCategorySecondService.findByPage(page);

    }

3.在B层开始进行分页配置,获取分页的内容

//分页查询二级分类的方法B层-Ares-2016年12月5日18:50:06
    public PageBean<CategorySecond> findByPage(Integer page) {
        //1.实例化一个PageBean
        PageBean<CategorySecond> pageBean = new PageBean<CategorySecond>();

        //2.设置每页显示的记录数
        pageBean.setPage(page);

        //3.设置每页显示的最大记录数
        int limit = 10;
        pageBean.setLimit(limit);

        //4.设置总记录数,通过查询获得总记录数
        int totalCount = adminCategorySecondDao.findCount();
        pageBean.setTotalCount(totalCount);

        //5.设置总页数
        int totalPage = 0;
        if(totalCount % limit ==0){
            //如果总页数正好分配到整页数上面
            totalPage = totalCount /limit;
        }
        else
        {
            //如果总页数分配后的比完整的多一个
            totalPage = totalCount /limit + 1;
        }
        pageBean.setTotalPage(totalPage);

        //6.设置每页显示的数据集合
        int begin = (page -1 )*limit;
        List<CategorySecond> list =adminCategorySecondDao.findByPage(begin ,limit);
        pageBean.setList(list);
        return pageBean;
    }

4.D层查询的方法

//通过查询获得总记录数
    public int findCount() {
        String hql = "select count(*) from CategorySecond";
        List<Long> list = this.getHibernateTemplate().find(hql);
        if(list!=null && list.size()>0){
            return list.get(0).intValue(); 
        }
        return 0;
    }

    //设置每页显示的数据集合
    public List<CategorySecond> findByPage(int begin, int limit) {
        String hql = "form CategorySecond order by csid desc";
        List<CategorySecond> list = this.getHibernateTemplate().execute(new PageHibernateCallback<CategorySecond>(hql,null,begin,limit));
        return list;
    }

5.完成U层的方法调用,将查询到的值放到值栈中。

//查询二级分类的方法
    public String findAll(){
        PageBean<CategorySecond> pageBean = adminCategorySecondService.findByPage(page);
        //将pageBean中的数据存储到值栈,在页面显示
        ActionContext.getContext().getValueStack().set("pageBean",pageBean );
        return "findAll";
    }

6.查询到后,需要在Struts中配置网页跳转

<!-- 配置后台二级分类管理的ACtion -->
        <action name="adminCategorySecond_*" class="adminCategorySecondAction" method="{1}" >
            <result name="findAll">/admin/categorysecond/list.jsp</result> 
        </action>

7.在页面显示

<table cellSpacing="1" cellPadding="0" width="100%" align="center" bgColor="#f5fafe" border="0">
                <TBODY>
                    <tr>
                        <td class="ta_01" align="center" bgColor="#afd1f3">
                            <strong>二级分类 列 表</strong>
                        </TD>
                    </tr>
                    <tr>
                        <td class="ta_01" align="right">
                            <button type="button" id="add" name="add" value="添加" class="button_add" onclick="addCategory()">
添加
</button>

                        </td>
                    </tr>
                    <tr>
                        <td class="ta_01" align="center" bgColor="#f5fafe">
                            <table cellspacing="0" cellpadding="1" rules="all"
                                bordercolor="gray" border="1" id="DataGrid1"
                                style="BORDER-RIGHT: gray 1px solid; BORDER-TOP: gray 1px solid; BORDER-LEFT: gray 1px solid; WIDTH: 100%; WORD-BREAK: break-all; BORDER-BOTTOM: gray 1px solid; BORDER-COLLAPSE: collapse; BACKGROUND-COLOR: #f5fafe; WORD-WRAP: break-word">
                                <tr
                                    style="FONT-WEIGHT: bold; FONT-SIZE: 12pt; HEIGHT: 25px; BACKGROUND-COLOR: #afd1f3">

                                    <td align="center" width="18%">
                                        序号
                                    </td>
                                    <td align="center" width="17%">
                                        二级分类名称
                                    </td>
                                    <td width="7%" align="center">
                                        编辑
                                    </td>
                                    <td width="7%" align="center">
                                        删除
                                    </td>
                                </tr>
                                <!-- 通过status里面的count属性获得序号 -->
                                <s:iterator var="cs" value="pageBean.list" status="status">
                                        <tr onmouseover="this.style.backgroundColor = 'white'"
                                            onmouseout="this.style.backgroundColor = '#F5FAFE';">
                                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                                width="18%">
                                                <s:property value="#status.count"/>
                                            </td>
                                            <td style="CURSOR: hand; HEIGHT: 22px" align="center"
                                                width="17%">
                                                <s:property value="#cs.csname"/>
                                            </td>
                                            <td align="center" style="HEIGHT: 22px">
                                                <a href="${pageContext.request.contextPath}/adminCategory_edit.action?cid=<s:property value="#c.cid"/>">
                                                    <img src="${pageContext.request.contextPath}/images/i_edit.gif" border="0" style="CURSOR: hand">
                                                </a>
                                            </td>

                                            <td align="center" style="HEIGHT: 22px">
                                                <a href="${pageContext.request.contextPath}/adminCategory_delete.action?cid=<s:property value="#c.cid"/>">
                                                    <img src="${pageContext.request.contextPath}/images/i_del.gif" width="16" height="16" border="0" style="CURSOR: hand">
                                                </a>
                                            </td>
                                        </tr>
                                    </s:iterator>   
                            </table>
                        </td>
                    </tr>
                    <tr align="center">
                        <td colspan="4">
                            第<s:property value="pageBean.page"/>/<s:property value="pageBean.totalPage"/>页  
                            <s:if test="pageBean.page != 1">
                                <a href="${pageContext.request.contextPath }/adminCategorySecond_findAll.action?page=1">首页</a> |
                                <a href="${pageContext.request.contextPath }/adminCategorySecond_findAll.action?page=<s:property value="pageBean.page-1"/>">上一页</a> |
                            </s:if>
                            <s:if test="pageBean.page != pageBean.totalPage">
                            <a href="${pageContext.request.contextPath }/adminCategorySecond_findAll.action?page=<s:property value="pageBean.page+1"/>">下一页</a> |
                            <a href="${pageContext.request.contextPath }/adminCategorySecond_findAll.action?page=<s:property value="pageBean.totalPage"/>">尾页</a> 
                            </s:if>
                        </td>
                    </tr>

                </TBODY>
            </table>

附:PageBean.java类:

package cn.itcast.shop.utils;

import java.util.List;

/*分页类封装
 * 2016年11月14日21:59:28
 * Ares
 * */

public class PageBean<T> {
    private int page;
    private int totalCount;
    private int totalPage;
    private int limit;
    private List<T> list;
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getLimit() {
        return limit;
    }
    public void setLimit(int limit) {
        this.limit = limit;
    }
    public List<T> getList() {
        return list;
    }
    public void setList(List<T> list) {
        this.list = list;
    }

}

附:PageHibernateCallback.java类:

package cn.itcast.shop.utils;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

public class PageHibernateCallback<T> implements HibernateCallback<List<T>>{

    private String hql;
    private Object[] params;
    private int startIndex;
    private int pageSize;


    public PageHibernateCallback(String hql, Object[] params,
            int startIndex, int pageSize) {
        super();
        this.hql = hql;
        this.params = params;
        this.startIndex = startIndex;
        this.pageSize = pageSize;
    }



    public List<T> doInHibernate(Session session) throws HibernateException,
            SQLException {
        //1 执行hql语句
        Query query = session.createQuery(hql);
        //2 实际参数
        if(params != null){
            for(int i = 0 ; i < params.length ; i ++){
                query.setParameter(i, params[i]);
            }
        }
        //3 分页
        query.setFirstResult(startIndex);
        query.setMaxResults(pageSize);

        return query.list();
    }

}

四、小结

      学习一定要联系旧知识,加油!每一次都是最后的分析,结果都是很好的加油!