Struts2+Hibernate分页显示实例

来源:互联网 发布:java鱼雷评测 编辑:程序博客网 时间:2024/05/22 01:34

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

版本:Struts2.1.8

         Hibernate3.2

         Oracle9g

具体代码如下:

1.Plan.java

[java] view plain copy
  1. package com.creattech.plan;  
  2.   
  3. public class Plan {  
  4.     private Integer id;  
  5.   
  6.     private String planId;  
  7.   
  8.     private Integer materialId;  
  9.   
  10.     private Integer materialIdl;  
  11.   
  12.     private String materialName;  
  13.   
  14.     private String speciFication;  
  15.   
  16.     private String unit;  
  17.   
  18.     private String planPurchase;  
  19.   
  20.     private Integer completionrate;  
  21.   
  22.     private Integer expuintPrice;  
  23.   
  24.     private Integer expunitPriceo;  
  25.   
  26.     private Integer exptotalPrice;  
  27.   
  28.     private Integer exptotalPriceo;  
  29.   
  30.     private Integer demandDepartment;  
  31.   
  32.   
  33.     public Integer getCompletionrate() {  
  34.         return completionrate;  
  35.     }  
  36.   
  37.     public void setCompletionrate(Integer completionrate) {  
  38.         this.completionrate = completionrate;  
  39.     }  
  40.   
  41.     public Integer getDemandDepartment() {  
  42.         return demandDepartment;  
  43.     }  
  44.   
  45.     public void setDemandDepartment(Integer demandDepartment) {  
  46.         this.demandDepartment = demandDepartment;  
  47.     }  
  48.   
  49.     public Integer getExptotalPrice() {  
  50.         return exptotalPrice;  
  51.     }  
  52.   
  53.     public void setExptotalPrice(Integer exptotalPrice) {  
  54.         this.exptotalPrice = exptotalPrice;  
  55.     }  
  56.   
  57.     public Integer getExptotalPriceo() {  
  58.         return exptotalPriceo;  
  59.     }  
  60.   
  61.     public void setExptotalPriceo(Integer exptotalPriceo) {  
  62.         this.exptotalPriceo = exptotalPriceo;  
  63.     }  
  64.   
  65.     public Integer getExpuintPrice() {  
  66.         return expuintPrice;  
  67.     }  
  68.   
  69.     public void setExpuintPrice(Integer expuintPrice) {  
  70.         this.expuintPrice = expuintPrice;  
  71.     }  
  72.   
  73.     public Integer getExpunitPriceo() {  
  74.         return expunitPriceo;  
  75.     }  
  76.   
  77.     public void setExpunitPriceo(Integer expunitPriceo) {  
  78.         this.expunitPriceo = expunitPriceo;  
  79.     }  
  80.   
  81.     public Integer getId() {  
  82.         return id;  
  83.     }  
  84.   
  85.     public void setId(Integer id) {  
  86.         this.id = id;  
  87.     }  
  88.   
  89.     public Integer getMaterialId() {  
  90.         return materialId;  
  91.     }  
  92.   
  93.     public void setMaterialId(Integer materialId) {  
  94.         this.materialId = materialId;  
  95.     }  
  96.   
  97.     public Integer getMaterialIdl() {  
  98.         return materialIdl;  
  99.     }  
  100.   
  101.     public void setMaterialIdl(Integer materialIdl) {  
  102.         this.materialIdl = materialIdl;  
  103.     }  
  104.   
  105.     public String getPlanId() {  
  106.         return planId;  
  107.     }  
  108.   
  109.     public void setPlanId(String planId) {  
  110.         this.planId = planId;  
  111.     }  
  112.   
  113.     public String getPlanPurchase() {  
  114.         return planPurchase;  
  115.     }  
  116.   
  117.     public void setPlanPurchase(String planPurchase) {  
  118.         this.planPurchase = planPurchase;  
  119.     }  
  120.   
  121.     public String getSpeciFication() {  
  122.         return speciFication;  
  123.     }  
  124.   
  125.     public void setSpeciFication(String speciFication) {  
  126.         this.speciFication = speciFication;  
  127.     }  
  128.   
  129.     public String getUnit() {  
  130.         return unit;  
  131.     }  
  132.   
  133.     public void setUnit(String unit) {  
  134.         this.unit = unit;  
  135.     }  
  136.   
  137.     public String getMaterialName() {  
  138.         return materialName;  
  139.     }  
  140.   
  141.     public void setMaterialName(String materialName) {  
  142.         this.materialName = materialName;  
  143.     }  
  144.   
  145. }  

 

2.Plan.hbm.xml

[java] view plain copy
  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.   
  6. <hibernate-mapping package="com.creattech.plan">  
  7.   
  8.     <class name="Plan" table="PLAN" lazy="false" >  
  9.   
  10.         <id name="id" column="ID">  
  11.             <generator class="native" />  
  12.         </id>  
  13.   
  14.         <property name="planId" column="PLANID" type="string" />  
  15.         <property name="materialId" column="MATERIALID" />  
  16.         <property name="materialIdl" column="MATERIALIDL" />  
  17.         <property name="materialName" column="MATERIALNAME" />  
  18.         <property name="speciFication" column="SPECIFICATION" />  
  19.         <property name="unit" column="UNIT" type="string" />  
  20.         <property name="planPurchase" column="PLANPURCHASE" />  
  21.         <property name="completionrate" column="COMPLETIONRATE" />  
  22.         <property name="expuintPrice" column="EXPUINTPRICE" />  
  23.         <property name="expunitPriceo" column="EXPUNITPRICEO" />  
  24.         <property name="exptotalPrice" column="EXPTOTALPRICE" />  
  25.         <property name="exptotalPriceo" column="EXPTOTALPRICEO" />  
  26.         <property name="demandDepartment" column="DEMANDDEPARTMENT" />  
  27.       
  28.   
  29.     </class>  
  30.   
  31. </hibernate-mapping>  

 

3.建表

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

 

4.PlanDao.java

[java] view plain copy
  1. package com.creattech.plan.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. import com.creattech.plan.Plan;  
  6.   
  7. public interface PlanDao {  
  8.   
  9.     public int getPlanTotalPage(int rowsPerPage);  
  10.   
  11.     public List<Plan> findPlantByPage(int page, int rowsPerPage);  
  12.   
  13.     public int getPlanNum();  
  14.   
  15. }  

 5.HibernateUtils.java

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

6.PlanDaoImpl.java

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

 

7.struts.xml

[xhtml] view plain copy
  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.   
  9.         <action name="showpage"  
  10.             class="com.com.creattech.plan.action.ShowAction">  
  11.             <result name="success" type="dispatcher">  
  12.                 /WEB-INF/jsp/showpage.jsp</result>  
  13.               
  14.         </action>  
  15.   
  16.     </package>  
  17.   
  18.   
  19. </struts>  

 8.ShowAction.java

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

9.showpage.jsp

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

 

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

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

0 0