本文转载至博客园
想着每天能学个新东西,今天准备了这个hibernate+struts2实现页面的分页功能,在这里,首先要感谢
http://www.blogjava.net/rongxh7/archive/2008/11/29/243456.html
这位博友发表的博文,通过对他代码的阅读,从而自己实现了网页分页功能,以下是源代码,如大家有需要,可以回复我,我会放出源代码供大家下载!
1.DAO层接口的设计,定义一个PersonDAO接口,里面声明了两个方法:
public interface PersonDAO { public List<Person> queryByPage(String hql, int offset, int pageSize); public int getAllRowCount(String hql); }
2.DAO层接口的实现类PersonDAOImpl类,将其两个方法实现出来:
public class PersonDAOImpl implements PersonDAO { /** * 通过hql语句得到数据库中记录总数 */ @Override public int getAllRowCount(String hql) { Session session = HibernateUtil.openSession(); Transaction tx = null; int allRows = 0; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); allRows = query.list().size(); tx.commit(); } catch (Exception e) { if(tx != null) { tx.rollback(); } e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } return allRows; } /** * 使用hibernate提供的分页功能,得到分页显示的数据 */ @SuppressWarnings("unchecked") @Override public List<Person> queryByPage(String hql, int offset, int pageSize) { Session session = HibernateUtil.openSession(); Transaction tx = null; List<Person> list = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql).setFirstResult(offset).setMaxResults(pageSize); list = query.list(); tx.commit(); } catch (Exception e) { if(tx != null) { tx.rollback(); } e.printStackTrace(); } finally { HibernateUtil.closeSession(session); } return list; } }
3.定义了一个PageBean(每一页所需要的内容都存放在这个PageBean里面),里面用来存放网页每一页显示的内容:
public class PageBean { private List<Person> list; //通过hql从数据库分页查询出来的list集合 private int allRows; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 public List<Person> getList() { return list; } public void setList(List<Person> list) { this.list = list; } public int getAllRows() { return allRows; } public void setAllRows(int allRows) { this.allRows = allRows; } 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; } /** * 得到总页数 * @param pageSize 每页记录数 * @param allRows 总记录数 * @return 总页数 */ public int getTotalPages(int pageSize, int allRows) { int totalPage = (allRows % pageSize == 0)? (allRows / pageSize): (allRows / pageSize) + 1; return totalPage; } /** * 得到当前开始记录号 * @param pageSize 每页记录数 * @param currentPage 当前页 * @return */ public int getCurrentPageOffset(int pageSize, int currentPage) { int offset = pageSize * (currentPage - 1); return offset; } /** * 得到当前页, 如果为0 则开始第一页,否则为当前页 * @param page * @return */ public int getCurPage(int page) { int currentPage = (page == 0)? 1: page; return currentPage; } }
4.Service层接口设计,定义一个PersonService接口,里面声明了一个方法,返回一个PageBean:
public interface PersonService { public PageBean getPageBean(int pageSize, int page); }
5.Service层接口实现类PersonServiceImpl类,实现唯一的方法:
public class PersonServiceImpl implements PersonService { private PersonDAO personDAO = new PersonDAOImpl(); /** * pageSize为每页显示的记录数 * page为当前显示的网页 */ @Override public PageBean getPageBean(int pageSize, int page) { PageBean pageBean = new PageBean(); String hql = "from Person"; int allRows = personDAO.getAllRowCount(hql); int totalPage = pageBean.getTotalPages(pageSize, allRows); int currentPage = pageBean.getCurPage(page); int offset = pageBean.getCurrentPageOffset(pageSize, currentPage); List<Person> list = personDAO.queryByPage(hql, offset, pageSize); pageBean.setList(list); pageBean.setAllRows(allRows); pageBean.setCurrentPage(currentPage); pageBean.setTotalPage(totalPage); return pageBean; } }
6.Action层设计,定义一个PersonAction:
public class PersonAction extends ActionSupport { private PersonService personService = new PersonServiceImpl(); private int page; public int getPage() { return page; } public void setPage(int page) { this.page = page; } @Override public String execute() throws Exception { //表示每页显示5条记录,page表示当前网页 PageBean pageBean = personService.getPageBean(5, page); HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("pageBean", pageBean); return SUCCESS; } }
7.辅助类设计,HibernateUtil:
public class HibernateUtil { private static SessionFactory sessionFactory; static { sessionFactory = new Configuration().configure().buildSessionFactory(); } public static Session openSession() { Session session = sessionFactory.openSession(); return session; } public static void closeSession(Session session) { if(session != null) { session.close(); } } }
8.最后也就是分页页面显示pagePerson.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'pagePerson.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript"> function validate() { var page = document.getElementsByName("page")[0].value; if(page > <s:property value="#request.pageBean.totalPage"/>) { alert("你输入的页数大于最大页数,页面将跳转到首页!"); window.document.location.href = "personAction"; return false; } return true; } </script> </head> <body> <h1><font color="blue">分页查询</font></h1><hr> <table border="1" align="center" bordercolor="yellow" width="50%"> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> </tr> <s:iterator value="#request.pageBean.list" id="person"> <tr> <th><s:property value="#person.id"/></th> <th><s:property value="#person.name"/></th> <th><s:property value="#person.age"/></th> </tr> </s:iterator> </table> <center> <font size="5">共<font color="red"><s:property value="#request.pageBean.totalPage"/></font>页 </font> <font size="5">共<font color="red"><s:property value="#request.pageBean.allRows"/></font>条记录</font><br><br> <s:if test="#request.pageBean.currentPage == 1"> 首页 上一页 </s:if> <s:else> <a href="personAction.action">首页</a> <a href="personAction.action?page=<s:property value="#request.pageBean.currentPage - 1"/>">上一页</a> </s:else> <s:if test="#request.pageBean.currentPage != #request.pageBean.totalPage"> <a href="personAction.action?page=<s:property value="#request.pageBean.currentPage + 1"/>">下一页</a> <a href="personAction.action?page=<s:property value="#request.pageBean.totalPage"/>">尾页</a> </s:if> <s:else> 下一页 尾页 </s:else> </center><br> <center> <form action="personAction" onsubmit="return validate();"> <font size="4">跳转至</font> <input type="text" size="2" name="page">页 <input type="submit" value="跳转"> </form> </center> </body> </html>
至此,hibernate+struts2实现网页分页功能代码部分就完毕了,像hibernate与struts的配置文件就不列出来了,那些都不是重点!
页面效果如下:
相关推荐
STRUTS2+HIBERNATE分页 实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码实现代码详细的分页实现代码
本篇文章主要介绍了使用hibernate和struts2实现分页功能,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
hibernate和struts完美应用和分页演示示例 <br>此工程是hibernate和struts完美应用和分页演示示例。 <br>特点: 一、使用MS SERVER 2000的pubs数据库,并写了演示用的数据库脚本 二、应用hibernate和...
实现简单的分页功能,用Struts 和hibernate实现
Struts+Hibernate实现分页.
一个简单的hibernate+struts2分页功能 希望对你有所帮助
本例子用Struts+Hibernate 开发,主要实现分页功能,运行环境Myeclipse+tomcate,附有数据库和jar包。下载后直接运行
hibernate+struts实现jsp增删改查分页! hibernate实现对数据库的操作,struts实现对表单的操作! 实现了分页功能! (注:本本项目中文会出现乱码,我还没有解决!附有数据库文件!)
好久没写过什么东西了,最近自己在学习Java WEB方面的东西,自己想用JSP和 Struts2 HIBERNATE来写一个自己工作室的网站练练手,稍微有些收获今天写出来,与大家一起分享一下:STRUTS2+HIBERNATE实现分页是JAVA WEB最...
这是用Struts+Hibernate实现的分页及查询
struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页struts spring hibernate 登陆 SSH整合分页功能 SSH整合分页
spring+hibernate+struts实现分页功能,应用软件eclipse、sqlserver
hibernate+struts实现jsp增删改以及分页功能! hibernate做数据操作,struts处理表单。 实现了分布功能。附数据库! (注:如果出现乱码,大家配置一下tomcat的编码方式与数据库的编码方式就行了!)
这是本人用hibernate+struts2做的一个登录注册的小例子,里面包含分页查询,hiberante的多种技术!
JAVA分页大全.doc ...hibernate实现分页技术.txt struts实现分页技术源代码txt Struts+hibernate制作分页的问题txt spring+struts+hibernate 分页问题.txt spring+hibernate分页技术.txt jsp实现分页技术.txt
Spring+Hibernate+Struts实现分页
青鸟租房系统 用Hibernate+struts 实现的 可以看看 。。。。
在struts+hibernate框架下实现动态分页,压缩包只包括JSP源码,测试前先架构好框架
hibernate_struts分页hibernate_struts分页hibernate_struts分页
ExtJs4.2+Mysql+Struts2+Hibernate3实现分页查询 1.libs目录缺少hibernate核心jar包 2.libs目录缺少struts jar 3.WebRoot目录缺少ExtJs4.2核心类库 以上信息我都在项目里面注明了,因为这些内容的文件太大了,CSDN不...