Struts2+Hibernate分页显示实例



Struts2+Hibernate分页显示实例.下面是一个struts2+hibernate的分页显示,主要是用到了hibernate的相应分页方法大大简单了分页的代码

版本:Struts2.1.8

Hibernate3.2

Oracle9g

具体代码如下:

1.Plan.java

 

[java] view plaincopy

  1. package com.creattech.plan;
  2. public class Plan {
  3.     private Integer id;
  4.     private String planId;
  5.     private Integer materialId;
  6.     private Integer materialIdl;
  7.     private String materialName;
  8.     private String speciFication;
  9.     private String unit;
  10.     private String planPurchase;
  11.     private Integer completionrate;
  12.     private Integer expuintPrice;
  13.     private Integer expunitPriceo;
  14.     private Integer exptotalPrice;
  15.     private Integer exptotalPriceo;
  16.     private Integer demandDepartment;
  17.     public Integer getCompletionrate() {
  18.         return completionrate;
  19.     }
  20.     public void setCompletionrate(Integer completionrate) {
  21.         this.completionrate = completionrate;
  22.     }
  23.     public Integer getDemandDepartment() {
  24.         return demandDepartment;
  25.     }
  26.     public void setDemandDepartment(Integer demandDepartment) {
  27.         this.demandDepartment = demandDepartment;
  28.     }
  29.     public Integer getExptotalPrice() {
  30.         return exptotalPrice;
  31.     }
  32.     public void setExptotalPrice(Integer exptotalPrice) {
  33.         this.exptotalPrice = exptotalPrice;
  34.     }
  35.     public Integer getExptotalPriceo() {
  36.         return exptotalPriceo;
  37.     }
  38.     public void setExptotalPriceo(Integer exptotalPriceo) {
  39.         this.exptotalPriceo = exptotalPriceo;
  40.     }
  41.     public Integer getExpuintPrice() {
  42.         return expuintPrice;
  43.     }
  44.     public void setExpuintPrice(Integer expuintPrice) {
  45.         this.expuintPrice = expuintPrice;
  46.     }
  47.     public Integer getExpunitPriceo() {
  48.         return expunitPriceo;
  49.     }
  50.     public void setExpunitPriceo(Integer expunitPriceo) {
  51.         this.expunitPriceo = expunitPriceo;
  52.     }
  53.     public Integer getId() {
  54.         return id;
  55.     }
  56.     public void setId(Integer id) {
  57.         this.id = id;
  58.     }
  59.     public Integer getMaterialId() {
  60.         return materialId;
  61.     }
  62.     public void setMaterialId(Integer materialId) {
  63.         this.materialId = materialId;
  64.     }
  65.     public Integer getMaterialIdl() {
  66.         return materialIdl;
  67.     }
  68.     public void setMaterialIdl(Integer materialIdl) {
  69.         this.materialIdl = materialIdl;
  70.     }
  71.     public String getPlanId() {
  72.         return planId;
  73.     }
  74.     public void setPlanId(String planId) {
  75.         this.planId = planId;
  76.     }
  77.     public String getPlanPurchase() {
  78.         return planPurchase;
  79.     }
  80.     public void setPlanPurchase(String planPurchase) {
  81.         this.planPurchase = planPurchase;
  82.     }
  83.     public String getSpeciFication() {
  84.         return speciFication;
  85.     }
  86.     public void setSpeciFication(String speciFication) {
  87.         this.speciFication = speciFication;
  88.     }
  89.     public String getUnit() {
  90.         return unit;
  91.     }
  92.     public void setUnit(String unit) {
  93.         this.unit = unit;
  94.     }
  95.     public String getMaterialName() {
  96.         return materialName;
  97.     }
  98.     public void setMaterialName(String materialName) {
  99.         this.materialName = materialName;
  100.     }
  101. }

 

 

2.Plan.hbm.xml

 

[java] view plaincopy

  1. <?xml version=”1.0″ encoding=’UTF-8′?>
  2. <!DOCTYPE hibernate-mapping PUBLIC
  3.                             ”-//Hibernate/Hibernate Mapping DTD 3.0//EN”
  4.                             ”http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd” >
  5. <hibernate-mapping package=”com.creattech.plan”>
  6.     <class name=”Plan” table=”PLAN” lazy=”false” >
  7.         <id name=”id” column=”ID”>
  8.             <generator class=”native” />
  9.         </id>
  10.         <property name=”planId” column=”PLANID” type=”string” />
  11.         <property name=”materialId” column=”MATERIALID” />
  12.         <property name=”materialIdl” column=”MATERIALIDL” />
  13.         <property name=”materialName” column=”MATERIALNAME” />
  14.         <property name=”speciFication” column=”SPECIFICATION” />
  15.         <property name=”unit” column=”UNIT” type=”string” />
  16.         <property name=”planPurchase” column=”PLANPURCHASE” />
  17.         <property name=”completionrate” column=”COMPLETIONRATE” />
  18.         <property name=”expuintPrice” column=”EXPUINTPRICE” />
  19.         <property name=”expunitPriceo” column=”EXPUNITPRICEO” />
  20.         <property name=”exptotalPrice” column=”EXPTOTALPRICE” />
  21.         <property name=”exptotalPriceo” column=”EXPTOTALPRICEO” />
  22.         <property name=”demandDepartment” column=”DEMANDDEPARTMENT” />
  23.     </class>
  24. </hibernate-mapping>

 

 

3.建表

大家可以根据实体类和配置文件自己建表

 

4.PlanDao.java

 

[java] view plaincopy

  1. package com.creattech.plan.dao;
  2. import java.util.List;
  3. import com.creattech.plan.Plan;
  4. public interface PlanDao {
  5.     public int getPlanTotalPage(int rowsPerPage);
  6.     public List<Plan> findPlantByPage(int page, int rowsPerPage);
  7.     public int getPlanNum();
  8. }

 

5.HibernateUtils.java


 

[java] view plaincopy

  1. package com.creattech.plan.dao.impl;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.cfg.Configuration;
  5. public class HibernateUtils {
  6.     private static Configuration conf;
  7.     private static SessionFactory factory;
  8.     static {
  9.         conf = new Configuration();
  10.         conf.configure();
  11.         factory = conf.buildSessionFactory();
  12.     }
  13.     public static Session getSession() {
  14.         return factory.openSession();
  15.     }
  16.     public static SessionFactory getSessionFactory() {
  17.         return factory;
  18.     }
  19.     public static void main(String[] args) {
  20.         Session session = HibernateUtils.getSession();
  21.         System.out.println(session);
  22.     }
  23. }

 

6.PlanDaoImpl.java

 

[java] view plaincopy

  1. package com.creattech.plan.dao.impl;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import org.hibernate.Query;
  5. import org.hibernate.Session;
  6. import org.hibernate.Transaction;
  7. import com.creattech.plan.Plan;
  8. import com.creattech.plan.dao.PlanDao;
  9. public class PlanDaoImpl implements PlanDao {
  10.     /**
  11.      * 查找并返回所有计划
  12.      */
  13.     public List<Plan> findPlantByPage(int page, int rowsPerPage) {
  14.         Session session = HibernateUtils.getSession();
  15.         Query query = session.createQuery(“from Plan order by planId desc”);
  16.         query.setMaxResults(rowsPerPage); // 每页最多显示几条
  17.         query.setFirstResult((page - 1) * rowsPerPage); // 每页从第几条记录开始
  18.         List<Plan> list = query.list();
  19.         for (int i = 0; i < list.size(); i++) {
  20.             System.out.println(“findPlantByPage:”
  21.                     + list.get(i).getMaterialName());
  22.         }
  23.         session.close();
  24.         return list;
  25.     }
  26.     /**
  27.      * 共多少页计划数据
  28.      */
  29.     public int getPlanTotalPage(int rowsPerPage) {
  30.         // System.out.println(“rowsPerPage:” + rowsPerPage);
  31.         int rows = 0;
  32.         String hql = ”select count(*) from Plan”;
  33.         Session session = HibernateUtils.getSession();
  34.         Query query = session.createQuery(hql);
  35.         rows = ((Integer) query.iterate().next()).intValue();
  36.         // System.out.println(“rows:” + rows);
  37.         session.close();
  38.         if (rows % rowsPerPage == 0) {
  39.             return rows / rowsPerPage;
  40.         } else {
  41.             return rows / rowsPerPage + 1;
  42.         }
  43.     }
  44.     public int getPlanNum() {
  45.         String hql = ”select count(*) from Plan ”;
  46.         int rows = 0;
  47.         Session session = HibernateUtils.getSession();
  48.         Query query = session.createQuery(hql);
  49.         rows = ((Integer) query.iterate().next()).intValue();
  50.         session.close();
  51.         return rows;
  52.     }
  53.     /**
  54.      * 条件查询后返回的计划总页数
  55.      */
  56.     public int getPlanTotalPage(int rowsPerPage, String type, String search) {
  57.         int rows = 0;
  58.         Session session = HibernateUtils.getSession();
  59.         String hql = ”select count(*) from Plan p where p.” + type
  60.                 + ” like :type”;
  61.         Query query = session.createQuery(hql);
  62.         query.setString(“type”, ”%” + search + ”%”);
  63.         rows = ((Integer) query.iterate().next()).intValue();
  64.         // System.out.println(“rows:” + rows);
  65.         session.close();
  66.         if (rows % rowsPerPage == 0) {
  67.             return rows / rowsPerPage;
  68.         } else {
  69.             return rows / rowsPerPage + 1;
  70.         }
  71.     }
  72.     /**
  73.      * 条件查询后返回的计划数据总数
  74.      */
  75.     public int getPlanNum(String type, String search) {
  76.         int rows = 0;
  77.         Session session = HibernateUtils.getSession();
  78.         String hql = ”select count(*) from Plan p where p.” + type
  79.                 + ” like :type”;
  80.         Query query = session.createQuery(hql);
  81.         query.setString(“type”, ”%” + search + ”%”);
  82.         rows = ((Integer) query.iterate().next()).intValue();
  83.         // System.out.println(“rows:” + rows);
  84.         session.close();
  85.         return rows;
  86.     }
  87. }

 

 

7.struts.xml

 

[xhtml] view plaincopy

  1. <?xml version=”1.0″ encoding=”UTF-8″?>
  2. <!DOCTYPE struts PUBLIC
  3.     ”-//Apache Software Foundation//DTD Struts Configuration 2.1//EN”
  4.     ”http://struts.apache.org/dtds/struts-2.1.dtd”>
  5. <struts>
  6.     <package name=”page” extends=”default”
  7.         namespace=”/”>
  8.         <action name=”showpage”
  9.             class=”com.com.creattech.plan.action.ShowAction”>
  10.             <result name=”success” type=”dispatcher”>
  11.                 /WEB-INF/jsp/showpage.jsp</result>
  12.         </action>
  13.     </package>
  14. </struts>

 

8.ShowAction.java

 

[java] view plaincopy

  1. package com.creattech.plan.action;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.servlet.http.HttpServletRequest;
  5. import com.creattech.plan.Plan;
  6. import com.creattech.plan.service.PlanService;
  7. import com.creattech.plan.service.impl.PlanServiceImpl;
  8. public class ShowAction {
  9.     PlanService ps = new PlanServiceImpl();
  10.     List<Plan> pagePlanList = new ArrayList();
  11.     private int rowsPerPage = 10;// 每页显示几条
  12.     private int page = 1; // 默认当前页
  13.     private int totalPage;// 总共多少页
  14.     private int planNum;// 总过多少条
  15.     public String show() {
  16.         System.out.println(“Page:” + page);
  17.         pagePlanList = ps.findPlantByPage(page, rowsPerPage);
  18.         totalPage = ps.getPlanTotalPage(rowsPerPage);
  19.         planNum = ps.getPlanNum();
  20.         return ”success”;
  21.     }
  22.     public int getPage() {
  23.         return page;
  24.     }
  25.     public void setPage(int page) {
  26.         this.page = page;
  27.     }
  28.     public int getRowsPerPage() {
  29.         return rowsPerPage;
  30.     }
  31.     public void setRowsPerPage(int rowsPerPage) {
  32.         this.rowsPerPage = rowsPerPage;
  33.     }
  34.     public int getTotalPage() {
  35.         return totalPage;
  36.     }
  37.     public void setTotalPage(int totalPage) {
  38.         this.totalPage = totalPage;
  39.     }
  40.     public List<Plan> getPagePlanList() {
  41.         return pagePlanList;
  42.     }
  43.     public void setPagePlanList(List<Plan> pagePlanList) {
  44.         this.pagePlanList = pagePlanList;
  45.     }
  46.     public int getPlanNum() {
  47.         return planNum;
  48.     }
  49.     public void setPlanNum(int planNum) {
  50.         this.planNum = planNum;
  51.     }
  52. }

 

9.showpage.jsp

 

[java] view plaincopy

  1. <%@ page contentType=”text/html; charset=utf-8″%>
  2. <%@ taglib prefix=”s” uri=”/struts-tags” %>
  3. <%
  4. request.setCharacterEncoding(“utf-8″);
  5. %>
  6. <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” ”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
  7. <html xmlns=”http://www.w3.org/1999/xhtml”>
  8. <head>
  9. <meta http-equiv=”Content-Type” content=”text/html; charset=GBK” />
  10. <link href=”../css/common.css” mce_href=”css/common.css” rel=”stylesheet” type=”text/css” />
  11. <link href=”../../css/css.css” mce_href=”css/css.css” rel=”stylesheet” type=”text/css”>
  12. <mce:style type=”text/css”><!–
  13. .none
  14. {
  15.     border-color:#FFFFFF;
  16.     border-top-style: none;
  17.     border-right-style: none;
  18.     border-bottom-style: none;
  19.     border-left-style: none;
  20. }
  21. .style1 {color: #0000FF}
  22. .style2 {color: #FF0000}
  23. –></mce:style><style type=”text/css” mce_bogus=”1″>.none
  24. {
  25.     border-color:#FFFFFF;
  26.     border-top-style: none;
  27.     border-right-style: none;
  28.     border-bottom-style: none;
  29.     border-left-style: none;
  30. }
  31. .style1 {color: #0000FF}
  32. .style2 {color: #FF0000}</style>
  33. <mce:script language=”javascript” src=”../js/edit.js” mce_src=”js/edit.js” /><!–
  34. <script language=”javascript” src=”../js/prototype-1.6.0.3.js” mce_src=”js/prototype-1.6.0.3.js” />
  35. <script language=”javascript”>
  36. function cClose(){
  37.     parent.window.close();
  38. }
  39. // –></mce:script>
  40. </head>
  41. <body>
  42. <table id=”nav” width=”850″ border=”0″ cellpadding=”0″ cellspacing=”0″
  43.     height=”30″>
  44. <tr>
  45. <td>
  46.   您现在的位置: <strong> 修改计划</strong>               <s:property value=”message”/></td>
  47.     </tr>
  48. </table>
  49. <form name=”form1″ id=”form1″ action=”" method=”get”>
  50.     <font color=”red”>    <ww:property value=”#session.msg”/></font>
  51.     <table  border=”1″ cellspacing=”0″ cellpadding=”0″ bordercolor=”#999999″ >
  52.         <tr bgcolor=”#DDDDDD”>
  53.          <td width=”40″ height=”30″><div align=”center”><strong>选择</strong></div></td>
  54.          <td width=”150″ ><div align=”center”><strong>计划编号</strong></div></td>
  55.          <td width=”150″><div align=”center”><strong>物资编码(8位)</strong></div></td>
  56.          <td width=”200″><div align=”center”><strong>物资名称</strong></div></td>
  57.          <td width=”300″><div align=”center”><strong>需求单位</strong></div></td>
  58.         </tr>
  59.     </table>
  60.     <table border=”0″>
  61.     <% int i=1; %>
  62.     <s:iterator value=”pagePlanList”>
  63.         <%if (i==1){ %>
  64.         <tr bgcolor=”rgb(214,223,247)”><%i=0;}else {i=1; %><tr bgcolor=”rgb(122,161,230)”><%} %>
  65.          <td width=”37″ height=”30″><div align=”center”><input type=”radio” name=”selectContract” value=”<s:property value=”planId” />” /></div></td>
  66.          <td width=”148″ ><div align=”center” ><a href=”editPlan.action?planId=<s:property value=” mce_href=”editPlan.action?planId=<s:property value=”planId” ></a>”><s:property value=”planId” /><a></a></div></td>
  67.          <td width=”148″><div align=”center”><s:property value=”materialId” /></div></td>
  68.          <td width=”198″><div align=”center”><s:property value=”materialName” /></div></td>
  69.          <td width=”300″><div align=”center”><s:property value=”demandDepartment” /></div></td>
  70.     </tr>
  71.     </s:iterator>
  72.     </table>
  73. <table width=”850″ border=”0″ cellpadding=”0″ cellspacing=”0″>
  74.     <tr>
  75.         <td bgcolor=”E3E3E3″ class=”wang” align=”center”>
  76.             <span class=”x2″ align=”center”>
  77.             <font color=”#0072BC”><b>
  78.                 当前第<s:property value=”page”/> 页,共<s:property value=”planNum”/>条记录, 共分<s:property value=”totalPage”/>页</b></font>
  79.             </span>
  80.         </td>
  81.     </tr>
  82.     <input type=”hidden” name=”maxNum” value=”">
  83.         <tr align=”center” valign=”top” >
  84.             <td height=”20″>
  85.                 <p align=”center”>
  86.             <span class=”x2″><a href=”showEditPlan.action?page=1″ mce_href=”showEditPlan.action?page=1″>首 页</a>
  87.             <s:if test=”page<=1″>
  88.             上一页<img src=”../images/aleft.gif” mce_src=”images/aleft.gif” width=”18″ height=”16″ border=”0″ align=”absbottom”>
  89.             </s:if>
  90.             <s:else>
  91.                 <a href=”showEditPlan.action?page=<s:property value=” mce_href=”showEditPlan.action?page=<s:property value=”page-1″></a>”>上一页
  92.                 <img src=”../images/aleft.gif” mce_src=”images/aleft.gif” width=”18″ height=”16″ border=”0″ align=”absbottom”></a>
  93.             </s:else>
  94.             <s:if test=”page>=totalPage”>
  95.             <img src=”../images/aright.gif” mce_src=”images/aright.gif” width=”18″ height=”16″ border=”0″ align=”absbottom”>下一页
  96.             </s:if>
  97.         <s:else>
  98.         <a href=”showEditPlan.action?page=<s:property value=” mce_href=”showEditPlan.action?page=<s:property value=”page+1″></a>”>
  99.                     <img src=”../images/aright.gif” mce_src=”images/aright.gif” width=”18″ height=”16″ border=”0″ align=”absbottom”>下一页</a>
  100.         </s:else>
  101.                     <a href=”showEditPlan.action?page=<s:property value=” mce_href=”showEditPlan.action?page=<s:property value=”totalPage”></a>”>最后一页</a>
  102.             </span>
  103.                 </p>
  104.             </td>
  105.         </tr>
  106. </table>
  107. <hr width=”850″ align=”left”/>
  108.   <table  width=”100%”>
  109.        <tr bgcolor=”#ffffff”>
  110.         <td  align=”center” >
  111.             <input type=”hidden” value=”" id=”pId” name=”pId”/>
  112.             <input type=”submit”  value=”查看” style=”width:70px” onClick=”return rCheck()” />
  113.            <input type=”button”  value=”关闭” style=”width:70px” onClick=”cClose()” />
  114.         </td>
  115.         </tr>
  116. </table>
  117. </form>
  118. </body>
  119. </html>

 

 

PS:由于没有把CSS,JS等代码粘上来可能此实例不能直接使用,但核心代码以写了出来(PlanDaoImpl.java)

大家可以参考参考,如果有什么不明白的地方可以留言