用户文章页面和所有文章显示列表都需要用到分页显示。
分页显示常通过创建hibernatecallback实例,注入sessionfactory引用来实现。
常用的获得session的方法:
1.this.getSession() 是org.springframework.orm.hibernate3.support.HibernateDaoSupport 中的一个方法,它可以从当前事务或者一个新的事务获得一个hibernate session,需要程序维护session(手动关闭session)
2.this.getHibernateTemplate().getSessionFactory().getCurrentSession()/openSession()则从spring中获取session
getCurrentSession()创建的Session会绑定到当前的线程中去、而采用OpenSession()则不会。
采用getCurrentSession()创建的Session在commit或rollback后会自动关闭,采用OpenSession()必须手动关闭
3.通过new HibernateCallback(){XXX}获得的session是当前线程中的session
4.hibernateTemplate会确保当前hibernate的session对象的正确打开和关闭,并直接参与到事物管理中去,template实例不仅是线程安全的同时也是可重用的
5.不提倡用getSession()来获得session实现,因为这样拿到的session是hibernate最原始的session不享有spring提供的模板支持需要手动的关闭session,可以使用new HibernateCallback(){xxx}得到session
首先创建page pojo类:
public class Page {
// 1.每页显示数量(everyPage)
private int everyPage;
// 2.总记录数(totalCount)
private int totalCount;
// 3.总页数(totalPage)
private int totalPage;
// 4.当前页(currentPage)
private int currentPage;
// 5.起始点(beginIndex)
private int beginIndex;
// 6.是否有上一页(hasPrePage)
private boolean hasPrePage;
// 7.是否有下一页(hasNextPage)
private boolean hasNextPage;
public Page(int everyPage, int totalCount, int totalPage, int currentPage,int beginIndex, boolean hasPrePage, boolean hasNextPage) {this.everyPage = everyPage;this.totalCount = totalCount;this.totalPage = totalPage;this.currentPage = currentPage;this.beginIndex = beginIndex;this.hasPrePage = hasPrePage;this.hasNextPage = hasNextPage;}//构造函数,默认public Page(){}public int getEveryPage() {return everyPage;}public void setEveryPage(int everyPage) {this.everyPage = everyPage;}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 getCurrentPage() {return currentPage;}public void setCurrentPage(int currentPage) {this.currentPage = currentPage;}public int getBeginIndex() {return beginIndex;}public void setBeginIndex(int beginIndex) {this.beginIndex = beginIndex;}public boolean isHasPrePage() {return hasPrePage;}public void setHasPrePage(boolean hasPrePage) {this.hasPrePage = hasPrePage;}public boolean isHasNextPage() {return hasNextPage;}public void setHasNextPage(boolean hasNextPage) {this.hasNextPage = hasNextPage;}}//构造方法,对所有属性进行设置
dao实现类代码:
public List<Article> pageQueryAllArticles(final Page page) { return this.getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(Session session) throws HibernateException, SQLException { Query query = session.createQuery("select article from Article article order by article.date desc"); query.setMaxResults(page.getEveryPage()); query.setFirstResult(page.getBeginIndex()); return query.list(); } }); }
jsp页面分页显示代码:<tr> <td align="center"> <s:if test="#request.page.hasPrePage"> <a href="showAllArticle.action?currentPage=1">首页</a> <a href="showAllArticle.action?currentPage=${page.currentPage -1 }">上一页</a> </s:if> <s:else> 首页 上一页 </s:else> <s:if test="#request.page.hasNextPage"> <a href="showAllArticle.action?currentPage=${page.currentPage + 1 }">下一页</a> <a href="showAllArticle.action?currentPage=${page.totalPage }">尾页</a> </s:if> <s:else> 下一页 尾页 </s:else> </td> </tr>
页面简陋,功能还不完善,欢迎提出修改意见