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="#">></a><br/>
共<s:property value="pagebean.allRow" />条记录
共<s:property value="pagebean.totalPage" />页
当前第<s:property value="pagebean.currentPage" />页
<br />