| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 652 人关注过本帖
标题:Web项目SSH中实现分页
只看楼主 加入收藏
张永亮
Rank: 2
等 级:论坛游民
帖 子:2
专家分:20
注 册:2011-12-25
收藏
 问题点数:0 回复次数:3 
Web项目SSH中实现分页
首相建立分页实体类实现封装
package com.yonliang.entity;

import java.util.List;

public class PageBean {
private List list; // 返回某一页的所有集合;
private int allRow ;//总记录数
private int totalPage; // 总页数
private int currentPage; //当前页
private int pageSize; //每页记录数
private boolean isFirstPage;//是否为第一页
private boolean isLastPage;//是否为最后一页
private boolean hasPreviousPage;// 是否有前一页
private boolean hasNextPage;// 是否有下一页
public List getList() {
    return list;
}
public void setList(List list) {
    this.list = list;
}
public int getAllRow() {
    return allRow;
}
public void setAllRow(int allRow) {
    this.allRow = allRow;
}
public int getTotalPage() {
    return totalPage;
}
public void setTotalPage(int totalPage) {
    this.totalPage = totalPage;
}
public int getCurrentPage() {
    return currentPage;
}
public void setCurrentPage(int currentPage) {
    this.currentPage = currentPage;
}
public int getPageSize() {
    return pageSize;
}
public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}
/*
 * 以下判断页的信息 ,只需getter方法即可
 */
public boolean isFirstPage() {
    return (currentPage==1); //如果是当前页
}
public boolean isLastPage() {
    return currentPage==totalPage; //如果是最后一页
}
public boolean isHasPreviousPage() {
    return currentPage!=1;       //如果当前页不是第一页
}
public boolean isHasNextPage() {
    return currentPage!=totalPage; //如果当前页不是最后一页
}
private void init() { //初始化分页信息
    // TODO Auto-generated method stub
    this.isFirstPage=isFirstPage();
    this.isLastPage=isLastPage();
    this.hasNextPage=isHasNextPage();
    this.hasPreviousPage=isHasPreviousPage();
}
/*
 * 计算总页数, 宫外不通过类直接调用
 */
public static int countTotalPage(final int pageSize,final int allow){
    int totalpage=allow%pageSize==0?allow/pageSize:allow/pageSize+1;
    return totalpage;
}
/*
 * 计算当前页开始记录的记录号
 */
public static int  countOffset(final int pageSize, final int currentPage){
    final int offset=pageSize*(currentPage-1);
      return offset;
}
/*
 * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替  
 */
public static int countCurrentPage(int page){
    int curpage=(page==0?1:page);
    return curpage;
}
}
建立分页dao层BooksDao接口
public interface BooksDao {
public List queryPage(final String hql,final int offset,final int length);//分页查询
public int getAllRowCount(String hql);//获得总记录数
}
BooksDaoImp实现方法
package com.yongliang.dao.imp;

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;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yongliang.dao.BooksDao;

public class BooksDaoImp extends HibernateDaoSupport implements BooksDao {

    public List queryPage(final String hql,final  int offset,final  int length) {
        // TODO Auto-generated method stub
        //匿名内部类 按分页查询
        List list=getHibernateTemplate().executeFind(new HibernateCallback() {
            
            public Object doInHibernate(Session s) throws HibernateException,
                    SQLException {
                // TODO Auto-generated method stub
                Query q=s.createQuery(hql);
                q.setFirstResult(offset);
                q.setMaxResults(length);
                List list=q.list();
                return list;
            }
        });
        return list;
    }

    public int getAllRowCount(String hql) {
        // TODO Auto-generated method stub   
        return getHibernateTemplate().find(hql).size();
    }

}
建立service层BookServices dao接口
package com.yongliang.service;
import com.yonliang.entity.PageBean;
public interface BookServices {
public PageBean queryPage(int pageSize,int page);
}
BookServicesImp实现方法
package com.yongliang.service.imp;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.yongliang.dao.BooksDao;
import com.yonliang.entity.PageBean;
import com.yonliang.entity.TBook;

public class BookServicesImp extends HibernateDaoSupport implements com.yongliang.service.BookServices{
private   BooksDao bookdao;

    public void setBookdao(BooksDao bookdao) {
    this.bookdao = bookdao;
}
    public PageBean queryPage(int pageSize, int page) {
        // TODO Auto-generated method stub
        final String hql="from TBook";
        int  allRow =bookdao.getAllRowCount(hql);//共多少条记录
        int totalPage=PageBean.countTotalPage(pageSize, allRow);
        final  int offset=PageBean.countOffset(pageSize, page);//当前记录号
        final int length=pageSize;//每页记录数
        final int currentPage=PageBean.countCurrentPage(page);//当前第几页
        List<TBook> list=bookdao.queryPage(hql, offset, length);
        PageBean  pagebean=new PageBean();
        pagebean.setAllRow(allRow);
        pagebean.setList(list);
        pagebean.setCurrentPage(page);
        pagebean.setPageSize(pageSize);
        pagebean.setTotalPage(totalPage);
        return pagebean;
    }


}
在Spring加入配置
    <bean id="bookdao" class="com.yongliang.dao.imp.BooksDaoImp">
        <property name="sessionFactory" ref="sessionfactory"></property>
    </bean>
    <bean id="servicesdao" class="com.yongliang.service.imp.BookServicesImp">
    <property name="bookdao" ref="bookdao"></property>
        <property name="sessionFactory" ref="sessionfactory"></property>
    </bean>
<bean id="bookAction" class="com.yongliang.action.BookInfoAction">
        <property name="bookservices" ref="servicesdao"></property>
    </bean>
在action 实现调用方法
package com.yongliang.action;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yongliang.dao.BooksDao;
import com.yongliang.service.BookServices;
import com.yonliang.entity.PageBean;

public class BookInfoAction extends ActionSupport {
    private BookServices bookservices;
    private int page;
    private PageBean pagebean;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public PageBean getPagebean() {
        return pagebean;
    }

    public void setPagebean(PageBean pagebean) {
        this.pagebean = pagebean;
    }

    public void setBookservices(BookServices bookservices) {
        this.bookservices = bookservices;
    }

    public String query() {
        // TODO Auto-generated method stub
        this.pagebean = bookservices.queryPage(3, page);
        Map map=ActionContext.getContext().getSession();
        map.put("PAGEBEAN", this.pagebean);
        List booklist=pagebean.getList();
        map.put("BOOKLIST", booklist);
        return "ok";
        
    }
}
在jsp实现页
<s:if test="%{pagebean.currentPage == 1}">
          首页 上一页
        </s:if>
                        <s:else>
                            <a href="pagebean!query.action?page=1">首页</a>
                            <a
                                href="pagebean!query.action?page=<s:property value="%{pagebean.currentPage-1}"/>">上一页</a>
                        </s:else>
                        <s:if test="%{pagebean.currentPage!= pagebean.totalPage}">
                            <a
                                href="pagebean!query.action?page=<s:property value="%{pagebean.currentPage+1}"/>">下一页</a>
                            <a
                                href="pagebean!query.action?page=<s:property value="pagebean.totalPage"/>">尾页</a>
                        </s:if>
                        <s:else>
            下一页 尾页
        </s:else>
                        <a href="#">&gt;</a><br/>
                        共<s:property value="pagebean.allRow" />条记录
                        共<s:property value="pagebean.totalPage" />页
                         当前第<s:property value="pagebean.currentPage" />页
                        <br />
搜索更多相关主题的帖子: private package public return import 
2013-11-30 10:52
zb670984722
Rank: 2
来 自:北京海淀
等 级:论坛游民
威 望:1
帖 子:59
专家分:37
注 册:2013-4-11
收藏
得分:0 
赞一个
2014-02-19 15:23
java小蚂蚁
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:上海
等 级:贵宾
威 望:18
帖 子:558
专家分:2186
注 册:2013-7-2
收藏
得分:0 

学海无涯#¥%……&*(
2014-02-21 10:45
我叫老黑、
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2014-3-1
收藏
得分:0 
给力
2014-03-01 10:55
快速回复:Web项目SSH中实现分页
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024729 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved