ITeye博客

spring+struts+hibernate分页实例

ceoajun · 2011-08-09 22:20 · hibernate · struts · spring · 编程语言 · Markdown

  1. package com.yizhou.common.test;
  2. /**
  3. *
  4. * Copyright @ 2008 YIZHOU SOFTWARE Co. Ltd.
  5. * All right reserved.
  6. *
  7. * @author xuxinlong
  8. *
  9. *  mail: longxx888@163.com
  10. *
  11. */
  12. public class PageBean {
  13. private int count = 0; // 记录总数
  14. private int pageSize = 20; // 每页显示记录数
  15. private int pageCount = 0; // 总页数
  16. private int page = 1; // 当前页数
  17. private String totalCountSQL;// 得到总记录数sql语句
  18. private String listSQL;// 得到查询记录sql语句
  19. public int getCount() {
  20. return count;
  21. }
  22. public void setCount(int count) {
  23. if (pageSize != 0) {
  24. pageCount = count / pageSize;
  25. if (count % pageSize != 0) {
  26. pageCount++;
  27. }
  28. }
  29. this.count = count;
  30. }
  31. public String getListSQL() {
  32. return listSQL;
  33. }
  34. public void setListSQL(String listSQL) {
  35. this.listSQL = listSQL;
  36. }
  37. public int getPage() {
  38. return page;
  39. }
  40. public void setPage(int page) {
  41. this.page = page;
  42. }
  43. public int getPageCount() {
  44. return pageCount;
  45. }
  46. public void setPageCount(int pageCount) {
  47. this.pageCount = pageCount;
  48. }
  49. public int getPageSize() {
  50. return pageSize;
  51. }
  52. public void setPageSize(int pageSize) {
  53. this.pageSize = pageSize;
  54. }
  55. public String getTotalCountSQL() {
  56. return totalCountSQL;
  57. }
  58. public void setTotalCountSQL(String totalCountSQL) {
  59. this.totalCountSQL = totalCountSQL;
  60. }
  61. }

新建个接口

  1. package com.yizhou.common.test;
  2. import java.io.Serializable;
  3. import java.util.List;
  4. /**
  5. *
  6. * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
  7. * All right reserved.
  8. *
  9. * @author xuxinlong
  10. *
  11. *  mail: longxx888@163.com
  12. *
  13. */
  14. public interface PaginateInterface extends Serializable {
  15. public List getList(PageBean page);
  16. public String getToolsMenu(PageBean page);
  17. public Long getTotalCount(PageBean p, String str[], Object ob2[])
  18. throws Exception;
  19. public Long getTotalCount(PageBean page) throws Exception;
  20. public List getList(PageBean page, String str[], Object ob2[])
  21. throws Exception;
  22. }
  1. package com.yizhou.common.test;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.hibernate.Query;
  5. import org.hibernate.Session;
  6. import org.springframework.orm.hibernate3.HibernateCallback;
  7. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
  8. /**
  9. *分页核心类
  10. * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
  11. * All right reserved.
  12. *
  13. * @author xuxinlong
  14. *
  15. *  mail: longxx888@163.com
  16. *
  17. */
  18. public class Paginate extends HibernateDaoSupport implements PaginateInterface {
  19. /**
  20. * 显示用的分页信息
  21. */
  22. public String getToolsMenu(PageBean p) {
  23. StringBuffer str = new StringBuffer("");
  24. int next, prev;
  25. prev = p.getPage() - 1;
  26. next = p.getPage() + 1;
  27. if (p.getPage() > 1) {
  28. str
  29. .append("<a href=\"#\" onclick=\"document.forms(0).jumpPage.value=1;document.forms(0).submit();\">首页</a> ");
  30. } else {
  31. //str.append("<a href=\"#\">首页</a> ");
  32. str.append("首页 ");
  33. }
  34. if (p.getPage() > 1) {
  35. str.append("<a href=\"#\" onclick='document.forms(0).jumpPage.value="
  36. + prev + ";document.forms(0).submit();'>上页</a> ");
  37. } else {
  38. //str.append("<a href=\"#\">上页</a> ");
  39. str.append("上页 ");
  40. }
  41. if (p.getPage() < p.getPageCount()) {
  42. str.append("<a href=\"#\" onclick='document.forms(0).jumpPage.value="
  43. + next + ";document.forms(0).submit();'>下页</a> ");
  44. } else {
  45. //str.append("<a href=\"#\" >下页</a> ");
  46. str.append("下页 ");
  47. }
  48. if (p.getPageCount() > 1 && p.getPage() != p.getPageCount()) {
  49. str.append("<a href=\"#\"  onclick='document.forms(0).jumpPage.value="
  50. + p.getPageCount()
  51. + ";document.forms(0).submit();'>末页</a>  ");
  52. } else {
  53. //str.append("<a href=\"#\" >末页</a>  ");
  54. str.append("末页  ");
  55. }
  56. str.append(" 共" + p.getCount() + "条记录");
  57. str
  58. .append("  每页<SELECT size=1 name=pagesize onchange='this.form.jumpPage.value=1;this.form.pageSize.value=this.value;this.form.submit();'>");
  59. if (p.getPageSize() == 3) {
  60. str.append("<OPTION value=3 selected>3</OPTION>");
  61. } else {
  62. str.append("<OPTION value=3>3</OPTION>");
  63. }
  64. if (p.getPageSize() == 10) {
  65. str.append("<OPTION value=10 selected>10</OPTION>");
  66. } else {
  67. str.append("<OPTION value=10>10</OPTION>");
  68. }
  69. if (p.getPageSize() == 20) {
  70. str.append("<OPTION value=20 selected>20</OPTION>");
  71. } else {
  72. str.append("<OPTION value=20>20</OPTION>");
  73. }
  74. if (p.getPageSize() == 50) {
  75. str.append("<OPTION value=50 selected>50</OPTION>");
  76. } else {
  77. str.append("<OPTION value=50>50</OPTION>");
  78. }
  79. if (p.getPageSize() == 100) {
  80. str.append("<OPTION value=100 selected>100</OPTION>");
  81. } else {
  82. str.append("<OPTION value=100>100</OPTION>");
  83. }
  84. str.append("</SELECT>");
  85. str.append("条 分" + p.getPageCount() + "页显示 转到");
  86. str
  87. .append("<SELECT size=1 name=Pagelist onchange='this.form.jumpPage.value=this.value;this.form.submit();'>");
  88. for (int i = 1; i < p.getPageCount() + 1; i++) {
  89. if (i == p.getPage()) {
  90. str.append("<OPTION value=" + i + " selected>" + i
  91. + "</OPTION>");
  92. } else {
  93. str.append("<OPTION value=" + i + ">" + i + "</OPTION>");
  94. }
  95. }
  96. str.append("</SELECT>页");
  97. str.append("<INPUT type=hidden  value=" + p.getPage()
  98. + " name=\"pages\" > ");
  99. str.append("<INPUT type=hidden  value=" + p.getPageSize()
  100. + " name=\"pageSize\"> ");
  101. return str.toString();
  102. }
  103. /**
  104. * 获取总条数
  105. */
  106. public Long getTotalCount(PageBean p) throws Exception {
  107. List list = getHibernateTemplate().find(p.getTotalCountSQL());
  108. long count = 0;
  109. if (list.size() > 0) {
  110. count = new Long(""+list.get(0));
  111. }
  112. return count;
  113. }
  114. /**
  115. * 查询信息进行分页
  116. */
  117. public List getList(final PageBean p) {
  118. return this.getHibernateTemplate().executeFind(new HibernateCallback(){
  119. public Object doInHibernate(Session session){
  120. Query q = session.createQuery(p.getListSQL());
  121. q.setFirstResult((p.getPage() - 1) * p.getPageSize());
  122. q.setMaxResults(p.getPageSize());
  123. return q.list();
  124. }
  125. });
  126. }
  127. /**
  128. * 查询信息进行分页  带有参数的
  129. */
  130. public List getList(final PageBean p,final String str[], final Object ob2[]) {
  131. return this.getHibernateTemplate().executeFind(new HibernateCallback(){
  132. public Object doInHibernate(Session session){
  133. Query q = session.createQuery(p.getListSQL());
  134. if(str!=null){
  135. for (int i = 0; i < str.length; i++) {
  136. q.setParameter(str[i], ob2[i]);
  137. }
  138. }
  139. q.setFirstResult((p.getPage() - 1) * p.getPageSize());
  140. q.setMaxResults(p.getPageSize());
  141. return q.list();
  142. }
  143. });
  144. }
  145. /**
  146. * 获取总条数   带有参数的
  147. */
  148. public Long getTotalCount(PageBean p, String str[], Object ob2[])throws Exception {
  149. List list=new ArrayList();
  150. if(str!=null && str.length>0){
  151. list = getHibernateTemplate().findByNamedParam(p.getTotalCountSQL(), str, ob2);
  152. }else{
  153. list=this.getHibernateTemplate().find(p.getTotalCountSQL());
  154. }
  155. long count = 0;
  156. if (list.size() > 0) {
  157. count = (Long)list.get(0);
  158. }
  159. return count;
  160. }
  161. }

Action的调用

  1. package com.yizhou.common.test;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.apache.struts.action.Action;
  7. import org.apache.struts.action.ActionForm;
  8. import org.apache.struts.action.ActionForward;
  9. import org.apache.struts.action.ActionMapping;
  10. /**
  11. * 分页代码示例 调用
  12. * Copyright @ 2008 NANJING YIZHOU SOFTWARE Co. Ltd.
  13. * All right reserved.
  14. *
  15. * @author xuxinlong
  16. *
  17. */
  18. public class SplitPageAction extends Action {
  19. private PaginateInterface pageinate;
  20. public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
  21. HttpSession session=request.getSession();
  22. Object obj=session.getAttribute("KDUser");
  23. if(obj!=null){
  24. PageBean pb=new PageBean();
  25. String jumpPage=request.getParameter("jumpPage");
  26. String pageSize=request.getParameter("pageSize");
  27. if(jumpPage!=null && !"".equals(jumpPage) && pageSize!=null && !"".equals(pageSize)){
  28. pb.setPageSize(new Integer(pageSize));
  29. }else{
  30. jumpPage="1";
  31. }
  32. String strSqlCnt="select count(*) from TUsertable";
  33. String strSqlInfo="select u from TUsertable u";
  34. pb.setTotalCountSQL(strSqlCnt);
  35. pb.setListSQL(strSqlInfo);
  36. pb.setPage(new Integer(jumpPage));
  37. pb.setCount(this.pageinate.getTotalCount(pb).intValue());
  38. List listUser=this.pageinate.getList(pb);
  39. request.setAttribute("info", this.pageinate.getToolsMenu(pb));
  40. request.setAttribute("listUser", listUser);
  41. return mapping.findForward("pagelist");
  42. }
  43. return mapping.findForward("error");
  44. }
  45. public PaginateInterface getPageinate() {
  46. return pageinate;
  47. }
  48. public void setPageinate(PaginateInterface pageinate) {
  49. this.pageinate = pageinate;
  50. }
  51. }

spring文件配置

  1. <bean id="pageinate" class="com.kingdo.common.test.Paginate">
  2. <property name="sessionFactory">
  3. <ref local="sessionFactory" />
  4. </property>
  5. </bean>
  6. <bean name="/splitPage" class="com.kingdo.common.test.SplitPageAction" singleton="false">
  7. <property name="pageinate">
  8. <ref bean="pageinate" />
  9. </property>
  10. </bean>

JSP中使用

  1. <%@ page language="java" pageEncoding="utf-8"%>
  2. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
  3. <html>
  4. <head>
  5. <title>分页演示</title>
  6. <link href="css/list.css" rel="stylesheet" type="text/css" />
  7. <link href="css/table.css" rel="stylesheet" type="text/css" />
  8. </head>
  9. <body> <center><font size="5">分页演示</font></center>
  10. <form action="splitPage.do" method="post" name="splitPageForm">
  11. <input type="hidden" name="jumpPage">
  12. <table width="96%" class="center" border="0" cellpadding="0" cellspacing="0">
  13. <tr class="table_tr">
  14. <td >
  15. <table cla

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。