分页的实现(附代码)

Crm管理系统—分页的实现

1.jsp的实现

注:有hidden项,其作用是保存当前页pageNum

<s:hidden id="pageNum" name="pageNum"
value="1"/>
<td align="right">
        <span><s:property value="pageBean.pageNum"/> / <s:property value="pageBean.totalPage"/></span>
        <span>
           <s:if test="pageBean.pageNum > 1">
            <a href="javascript:void(0)" onclick="showPage(1)">[首页]</a>&nbsp;&nbsp;
            <a href="javascript:void(0)" onclick="showPage(<s:property value="pageBean.pageNum - 1"/>)">[上一页]</a>&nbsp;&nbsp;
           </s:if>
              <%--动态显示页 --%>
             <s:iterator begin="pageBean.start" end="pageBean.end" var="num">
                <a href="javascript:void(0)" onclick="showPage(<s:property value="#num"/>)"><s:property value="#num"/></a>&nbsp;&nbsp;                       
             </s:iterator>

           <s:if test="pageBean.pageNum < pageBean.totalPage">
            <a href="javascript:void(0)" onclick="showPage(<s:property value="pageBean.pageNum + 1"/>)">[下一页]</a>&nbsp;&nbsp;
            <a href="javascript:void(0)" onclick="showPage(<s:property value="pageBean.totalPage"/>)">[尾页]</a>
           </s:if>
        </span>
    </td>
    <script type="text/javascript">
        function showPage(num){
            //1.修改隐藏域的值
           document.getElementById("pageNum").value=num;
            //2.提交表单
           document.forms[0].submit();
        }
    </script>

2.action的实现

public String findAll(){
        //分页+条件
        PageBean<CrmCourseType> pageBean = this.courseTypeService.findAll(courseType,pageNum,pageSize);
        ActionContext.getContext().getValueStack().set("pageBean", pageBean);
        return "findAll";
    }

3.service的实现

public PageBean<CrmCourseType> findAll(CrmCourseType courseType,
            int pageNum, int pageSize) {
        //拼凑查询条件(and...and...)
        //拼凑实际参数   可以重复,顺序(Object[] params)             
        StringBuilder builder = new StringBuilder();
        List<Object> paramList = new ArrayList<Object>();
        //过滤条件
        //1.课程类别        if(StringUtils.isNotBlank(courseType.getCourseName())){
            builder.append(" and courseName like ? ");
            paramList.add("%"+courseType.getCourseName()+"%");
        }
        //2.课程简介        if(StringUtils.isNotBlank(courseType.getRemark())){
            builder.append(" and remark like ? ");
            paramList.add("%"+courseType.getRemark()+"%");
        }
        //3.学时      if(StringUtils.isNotBlank(courseType.getTotalStart())){
            builder.append(" and total >= ? ");         paramList.add(Integer.parseInt(courseType.getTotalStart()));
        }       if(StringUtils.isNotBlank(courseType.getTotalEnd())){
            builder.append(" and total <= ? ");         paramList.add(Integer.parseInt(courseType.getTotalEnd()));
        }
        //4.费用      if(StringUtils.isNotBlank(courseType.getLessonCostStart())){
            builder.append(" and courseCost >= ? ");        paramList.add(Double.parseDouble(courseType.getLessonCostStart()));
        }       if(StringUtils.isNotBlank(courseType.getLessonCostEnd())){
            builder.append(" and courseCost <= ? ");            paramList.add(Double.parseDouble(courseType.getLessonCostEnd()));
        }       
        String condition = builder.toString();
        Object[] params = paramList.toArray();
    /////////////////////////////////////////
        //2.分页
        //2.1总记录数
        int totalRecord = this.courseTypeDao.getTotalRecord(condition,params);
        //2.2创建对象
        PageBean<CrmCourseType> pageBean = new PageBean<CrmCourseType>(pageNum, pageSize, totalRecord);
        //2.3分页数据
        List<CrmCourseType> data = this.courseTypeDao.findAllCourseType(condition, params,pageBean.getStartIndex(),pageBean.getPageSize());
        pageBean.setData(data);     
        return pageBean;
    }

4.dao层的实现

    public int getTotalRecord(String condition, Object[] params) {
        String hql = "select count(c) from CrmCourseType c where 1 = 1 "+condition;
        List<Long> list = this.getHibernateTemplate().find(hql,params);
        return list.get(0).intValue();
    }

    public List<CrmCourseType> findAllCourseType(String condition,
            Object[] params, int startIndex, int pageSize) {
        String hql = "from CrmCourseType where 1=1 " + condition;
        return this.getHibernateTemplate().execute(new PageHibernateCallback<CrmCourseType>().setHql(hql).setParams(params).setStartIndex(startIndex).setPageSize(pageSize));
    }

5.hql语句中的find不能实现分页,要重写其方法

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

    public PageHibernateCallback<T> setHql(String hql) {
        this.hql = hql;
        return this;
    }
    public PageHibernateCallback<T> setParams(Object[] params) {
        this.params = params;
        return this;
    }
    public PageHibernateCallback<T> setStartIndex(int startIndex) {
        this.startIndex = startIndex;
        return this;
    }
    public PageHibernateCallback<T> setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    @Override
    public List<T> doInHibernate(Session session) throws HibernateException,
            SQLException {
         //1.通过hql语句,获得Query对象
        Query queryObject = session.createQuery(hql);
        //2.条件设置        
            for (int i = 0; i < params.length; i++) {
                queryObject.setParameter(i, params[i]); 
        }
        //3.分页
        queryObject.setFirstResult(startIndex);
        queryObject.setMaxResults(pageSize);
        //4.查询所有
        return queryObject.list();
    }

6.Page domain的实现

//必选项
    private int pageNum;        //当前页
    private int pageSize;   //每页显示个数(固定值)
    private int totalRecord;//总记录数(查询数据库)   
    //计算项
    private int startIndex;     //开始索引(计算)
    private int totalPage;      //总分页数(计算)  
    //数据
    private List<T> data;      //分页数据   
    //动态显示数据 
    private int start;
    private int end;
    //构造函数
    public PageBean(int pageNum, int pageSize, int totalRecord) {
        super();
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.totalRecord = totalRecord;

        //计算
        this.startIndex = (this.pageNum - 1) * this.pageSize;
        this.totalPage = (this.totalRecord + this.pageSize - 1 ) / this.pageSize;

        //1.初始化--显示10个分页
        this.start = 1;
        this.end = 10;
        //2.初始化数据,---totalPage
        if(this.end > this.totalPage){
            this.end = this.totalPage;
        }else{
            //totalPage=22
            //当前页,前4后5
            this.start = pageNum - 4;
            this.end = pageNum + 5;
            //pageNum = 1
            if(this.start <1){
                this.start = 1;
                this.end = 10; 
            }
            //pageNum ==22
            if(this.end > totalPage){
                this.end = totalPage;
                this.start = totalPage - 9;
            }
        }

7. 分页的实现效果如下

分页的显示

猜你喜欢

转载自blog.csdn.net/lucky_zfx/article/details/79007702