生活随笔
收集整理的這篇文章主要介紹了
Struts2+Hibernate分页显示实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面是一個struts2+hibernate的分頁顯示,主要是用到了hibernate的相應分頁方法大大簡單了分頁的代碼
版本:Struts2.1.8
???????? Hibernate3.2
???????? Oracle9g
具體代碼如下:
1.Plan.java
[java] view plain
copy package?com.creattech.plan;????public?class?Plan?{??????private?Integer?id;????????private?String?planId;????????private?Integer?materialId;????????private?Integer?materialIdl;????????private?String?materialName;????????private?String?speciFication;????????private?String?unit;????????private?String?planPurchase;????????private?Integer?completionrate;????????private?Integer?expuintPrice;????????private?Integer?expunitPriceo;????????private?Integer?exptotalPrice;????????private?Integer?exptotalPriceo;????????private?Integer?demandDepartment;??????????public?Integer?getCompletionrate()?{??????????return?completionrate;??????}????????public?void?setCompletionrate(Integer?completionrate)?{??????????this.completionrate?=?completionrate;??????}????????public?Integer?getDemandDepartment()?{??????????return?demandDepartment;??????}????????public?void?setDemandDepartment(Integer?demandDepartment)?{??????????this.demandDepartment?=?demandDepartment;??????}????????public?Integer?getExptotalPrice()?{??????????return?exptotalPrice;??????}????????public?void?setExptotalPrice(Integer?exptotalPrice)?{??????????this.exptotalPrice?=?exptotalPrice;??????}????????public?Integer?getExptotalPriceo()?{??????????return?exptotalPriceo;??????}????????public?void?setExptotalPriceo(Integer?exptotalPriceo)?{??????????this.exptotalPriceo?=?exptotalPriceo;??????}????????public?Integer?getExpuintPrice()?{??????????return?expuintPrice;??????}????????public?void?setExpuintPrice(Integer?expuintPrice)?{??????????this.expuintPrice?=?expuintPrice;??????}????????public?Integer?getExpunitPriceo()?{??????????return?expunitPriceo;??????}????????public?void?setExpunitPriceo(Integer?expunitPriceo)?{??????????this.expunitPriceo?=?expunitPriceo;??????}????????public?Integer?getId()?{??????????return?id;??????}????????public?void?setId(Integer?id)?{??????????this.id?=?id;??????}????????public?Integer?getMaterialId()?{??????????return?materialId;??????}????????public?void?setMaterialId(Integer?materialId)?{??????????this.materialId?=?materialId;??????}????????public?Integer?getMaterialIdl()?{??????????return?materialIdl;??????}????????public?void?setMaterialIdl(Integer?materialIdl)?{??????????this.materialIdl?=?materialIdl;??????}????????public?String?getPlanId()?{??????????return?planId;??????}????????public?void?setPlanId(String?planId)?{??????????this.planId?=?planId;??????}????????public?String?getPlanPurchase()?{??????????return?planPurchase;??????}????????public?void?setPlanPurchase(String?planPurchase)?{??????????this.planPurchase?=?planPurchase;??????}????????public?String?getSpeciFication()?{??????????return?speciFication;??????}????????public?void?setSpeciFication(String?speciFication)?{??????????this.speciFication?=?speciFication;??????}????????public?String?getUnit()?{??????????return?unit;??????}????????public?void?setUnit(String?unit)?{??????????this.unit?=?unit;??????}????????public?String?getMaterialName()?{??????????return?materialName;??????}????????public?void?setMaterialName(String?materialName)?{??????????this.materialName?=?materialName;??????}????}?? ?
2.Plan.hbm.xml
[java] view plain
copy <?xml?version="1.0"?encoding='UTF-8'?>??<!DOCTYPE?hibernate-mapping?PUBLIC??????????????????????????????"-//Hibernate/Hibernate?Mapping?DTD?3.0//EN"??????????????????????????????"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"?>????<hibernate-mapping?package="com.creattech.plan">????????<class?name="Plan"?table="PLAN"?lazy="false"?>????????????<id?name="id"?column="ID">??????????????<generator?class="native"?/>??????????</id>????????????<property?name="planId"?column="PLANID"?type="string"?/>??????????<property?name="materialId"?column="MATERIALID"?/>??????????<property?name="materialIdl"?column="MATERIALIDL"?/>??????????<property?name="materialName"?column="MATERIALNAME"?/>??????????<property?name="speciFication"?column="SPECIFICATION"?/>??????????<property?name="unit"?column="UNIT"?type="string"?/>??????????<property?name="planPurchase"?column="PLANPURCHASE"?/>??????????<property?name="completionrate"?column="COMPLETIONRATE"?/>??????????<property?name="expuintPrice"?column="EXPUINTPRICE"?/>??????????<property?name="expunitPriceo"?column="EXPUNITPRICEO"?/>??????????<property?name="exptotalPrice"?column="EXPTOTALPRICE"?/>??????????<property?name="exptotalPriceo"?column="EXPTOTALPRICEO"?/>??????????<property?name="demandDepartment"?column="DEMANDDEPARTMENT"?/>??????????????</class>????</hibernate-mapping>?? ?
3.建表
???? 大家可以根據(jù)實體類和配置文件自己建表
?
4.PlanDao.java
[java] view plain
copy package?com.creattech.plan.dao;????import?java.util.List;????import?com.creattech.plan.Plan;????public?interface?PlanDao?{????????public?int?getPlanTotalPage(int?rowsPerPage);????????public?List<Plan>?findPlantByPage(int?page,?int?rowsPerPage);????????public?int?getPlanNum();????}?? ?5.HibernateUtils.java
[java] view plain
copy package?com.creattech.plan.dao.impl;????import?org.hibernate.Session;??import?org.hibernate.SessionFactory;??import?org.hibernate.cfg.Configuration;????public?class?HibernateUtils?{??????private?static?Configuration?conf;????????private?static?SessionFactory?factory;??????static?{??????????conf?=?new?Configuration();??????????conf.configure();??????????factory?=?conf.buildSessionFactory();??????}????????public?static?Session?getSession()?{??????????return?factory.openSession();??????}????????public?static?SessionFactory?getSessionFactory()?{??????????return?factory;??????}????????public?static?void?main(String[]?args)?{??????????Session?session?=?HibernateUtils.getSession();??????????System.out.println(session);??????}??}?? 6.PlanDaoImpl.java
[java] view plain
copy package?com.creattech.plan.dao.impl;????import?java.util.List;????import?javax.servlet.http.HttpServletRequest;????import?org.hibernate.Query;??import?org.hibernate.Session;??import?org.hibernate.Transaction;????import?com.creattech.plan.Plan;??import?com.creattech.plan.dao.PlanDao;????public?class?PlanDaoImpl?implements?PlanDao?{??????????????????public?List<Plan>?findPlantByPage(int?page,?int?rowsPerPage)?{??????????Session?session?=?HibernateUtils.getSession();??????????Query?query?=?session.createQuery("from?Plan?order?by?planId?desc");??????????query.setMaxResults(rowsPerPage);???????????query.setFirstResult((page?-?1)?*?rowsPerPage);???????????List<Plan>?list?=?query.list();??????????for?(int?i?=?0;?i?<?list.size();?i++)?{??????????????System.out.println("findPlantByPage:"??????????????????????+?list.get(i).getMaterialName());??????????}????????????session.close();????????????return?list;??????}????????????????public?int?getPlanTotalPage(int?rowsPerPage)?{????????????????????int?rows?=?0;??????????String?hql?=?"select?count(*)?from?Plan";??????????Session?session?=?HibernateUtils.getSession();??????????Query?query?=?session.createQuery(hql);????????????rows?=?((Integer)?query.iterate().next()).intValue();????????????????????session.close();??????????if?(rows?%?rowsPerPage?==?0)?{??????????????return?rows?/?rowsPerPage;??????????}?else?{??????????????return?rows?/?rowsPerPage?+?1;??????????}??????}????????public?int?getPlanNum()?{??????????String?hql?=?"select?count(*)?from?Plan?";??????????int?rows?=?0;??????????Session?session?=?HibernateUtils.getSession();??????????Query?query?=?session.createQuery(hql);????????????rows?=?((Integer)?query.iterate().next()).intValue();????????????session.close();??????????return?rows;??????}????????????????public?int?getPlanTotalPage(int?rowsPerPage,?String?type,?String?search)?{??????????int?rows?=?0;??????????Session?session?=?HibernateUtils.getSession();??????????String?hql?=?"select?count(*)?from?Plan?p?where?p."?+?type??????????????????+?"?like?:type";??????????Query?query?=?session.createQuery(hql);??????????query.setString("type",?"%"?+?search?+?"%");????????????rows?=?((Integer)?query.iterate().next()).intValue();????????????????????session.close();??????????if?(rows?%?rowsPerPage?==?0)?{??????????????return?rows?/?rowsPerPage;??????????}?else?{??????????????return?rows?/?rowsPerPage?+?1;??????????}??????}????????????????public?int?getPlanNum(String?type,?String?search)?{??????????int?rows?=?0;??????????Session?session?=?HibernateUtils.getSession();??????????String?hql?=?"select?count(*)?from?Plan?p?where?p."?+?type??????????????????+?"?like?:type";??????????Query?query?=?session.createQuery(hql);??????????query.setString("type",?"%"?+?search?+?"%");????????????rows?=?((Integer)?query.iterate().next()).intValue();????????????????????session.close();??????????return?rows;??????}????}?? ?
7.struts.xml
[xhtml] view plain
copy <?xml?version="1.0"?encoding="UTF-8"?>??<!DOCTYPE?struts?PUBLIC??????"-//Apache?Software?Foundation//DTD?Struts?Configuration?2.1//EN"??????"http://struts.apache.org/dtds/struts-2.1.dtd">??<struts>??????<package?name="page"?extends="default"??????????namespace="/">????????????<action?name="showpage"??????????????class="com.com.creattech.plan.action.ShowAction">??????????????<result?name="success"?type="dispatcher">??????????????????/WEB-INF/jsp/showpage.jsp</result>????????????????????????</action>????????</package>??????</struts>?? ?8.ShowAction.java
[java] view plain
copy package?com.creattech.plan.action;????import?java.util.ArrayList;??import?java.util.List;????import?javax.servlet.http.HttpServletRequest;????import?com.creattech.plan.Plan;??import?com.creattech.plan.service.PlanService;??import?com.creattech.plan.service.impl.PlanServiceImpl;????public?class?ShowAction?{????????PlanService?ps?=?new?PlanServiceImpl();????????List<Plan>?pagePlanList?=?new?ArrayList();????????private?int?rowsPerPage?=?10;????????private?int?page?=?1;?????????private?int?totalPage;????????private?int?planNum;????????public?String?show()?{????????????System.out.println("Page:"?+?page);??????????pagePlanList?=?ps.findPlantByPage(page,?rowsPerPage);??????????totalPage?=?ps.getPlanTotalPage(rowsPerPage);??????????planNum?=?ps.getPlanNum();????????????return?"success";??????}????????public?int?getPage()?{??????????return?page;??????}????????public?void?setPage(int?page)?{??????????this.page?=?page;??????}????????public?int?getRowsPerPage()?{??????????return?rowsPerPage;??????}????????public?void?setRowsPerPage(int?rowsPerPage)?{??????????this.rowsPerPage?=?rowsPerPage;??????}????????public?int?getTotalPage()?{??????????return?totalPage;??????}????????public?void?setTotalPage(int?totalPage)?{??????????this.totalPage?=?totalPage;??????}????????public?List<Plan>?getPagePlanList()?{??????????return?pagePlanList;??????}????????public?void?setPagePlanList(List<Plan>?pagePlanList)?{??????????this.pagePlanList?=?pagePlanList;??????}????????public?int?getPlanNum()?{??????????return?planNum;??????}????????public?void?setPlanNum(int?planNum)?{??????????this.planNum?=?planNum;??????}????}?? 9.showpage.jsp
[java] view plain
copy <%@?page?contentType="text/html;?charset=utf-8"%>????<%@?taglib?prefix="s"?uri="/struts-tags"?%>??<%??request.setCharacterEncoding("utf-8");??%>????<!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??????<html?xmlns="http://www.w3.org/1999/xhtml">??<head>??<meta?http-equiv="Content-Type"?content="text/html;?charset=GBK"?/>??<link?href="../css/common.css"?mce_href="css/common.css"?rel="stylesheet"?type="text/css"?/>????<link?href="../../css/css.css"?mce_href="css/css.css"?rel="stylesheet"?type="text/css">??<mce:style?type="text/css"><!--??.none??{??????border-color:#FFFFFF;??????border-top-style:?none;??????border-right-style:?none;??????border-bottom-style:?none;??????border-left-style:?none;??}??.style1?{color:?#0000FF}??.style2?{color:?#FF0000}??--></mce:style><style?type="text/css"?mce_bogus="1">.none??{??????border-color:#FFFFFF;??????border-top-style:?none;??????border-right-style:?none;??????border-bottom-style:?none;??????border-left-style:?none;??}??.style1?{color:?#0000FF}??.style2?{color:?#FF0000}</style>??<mce:script?language="javascript"?src="../js/edit.js"?mce_src="js/edit.js"?/><!--??<script?language="javascript"?src="../js/prototype-1.6.0.3.js"?mce_src="js/prototype-1.6.0.3.js"?/>??<script?language="javascript">??function?cClose(){??????parent.window.close();??}????</head>??<body>????<table?id="nav"?width="850"?border="0"?cellpadding="0"?cellspacing="0"??????height="30">??<tr>??<td>????您現(xiàn)在的位置:?<strong>?修改計劃</strong>???????????????<s:property?value="message"/></td>??????</tr>??</table>????????<form?name="form1"?id="form1"?action=""?method="get">??????<font?color="red">????<ww:property?value="#session.msg"/></font>??????<table??border="1"?cellspacing="0"?cellpadding="0"?bordercolor="#999999"?>??????????????????????????<tr?bgcolor="#DDDDDD">???????????<td?width="40"?height="30"><div?align="center"><strong>選擇</strong></div></td>???????????<td?width="150"?><div?align="center"><strong>計劃編號</strong></div></td>???????????<td?width="150"><div?align="center"><strong>物資編碼(8位)</strong></div></td>???????????<td?width="200"><div?align="center"><strong>物資名稱</strong></div></td>???????????<td?width="300"><div?align="center"><strong>需求單位</strong></div></td>??????????</tr>????????</table>??????????<table?border="0">??????<%?int?i=1;?%>??????<s:iterator?value="pagePlanList">??????????<%if?(i==1){?%>??????????<tr?bgcolor="rgb(214,223,247)"><%i=0;}else?{i=1;?%><tr?bgcolor="rgb(122,161,230)"><%}?%>???????????<td?width="37"?height="30"><div?align="center"><input?type="radio"?name="selectContract"?value="<s:property?value="planId"?/>"?/></div></td>???????????<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>???????????<td?width="148"><div?align="center"><s:property?value="materialId"?/></div></td>???????????<td?width="198"><div?align="center"><s:property?value="materialName"?/></div></td>???????????<td?width="300"><div?align="center"><s:property?value="demandDepartment"?/></div></td>??????</tr>??????</s:iterator>??????????????????????????</table>????<table?width="850"?border="0"?cellpadding="0"?cellspacing="0">??????<tr>??????????<td?bgcolor="E3E3E3"?class="wang"?align="center"> ????????????????????<span?class="x2"?align="center">??????????????<font?color="#0072BC"><b>??????????????????當前第<s:property?value="page"/>?頁,共<s:property?value="planNum"/>條記錄,?共分<s:property?value="totalPage"/>頁</b></font>??????????????</span>??????????</td>??????</tr>????????<input?type="hidden"?name="maxNum"?value="">??????????<tr?align="center"?valign="top"?>??????????????<td?height="20">??????????????????<p?align="center">????????????????????????????<span?class="x2"><a?href="showEditPlan.action?page=1"?mce_href="showEditPlan.action?page=1">首?頁</a>??????????????<s:if?test="page<=1">??????????????上一頁<img?src="../images/aleft.gif"?mce_src="images/aleft.gif"?width="18"?height="16"?border="0"?align="absbottom">??????????????</s:if>??????????????<s:else>??????????????????<a?href="showEditPlan.action?page=<s:property?value="?mce_href="showEditPlan.action?page=<s:property?value="page-1"></a>">上一頁??????????????????<img?src="../images/aleft.gif"?mce_src="images/aleft.gif"?width="18"?height="16"?border="0"?align="absbottom"></a>????????????????????????????????</s:else>????????????????????????????????????<s:if?test="page>=totalPage">??????????????<img?src="../images/aright.gif"?mce_src="images/aright.gif"?width="18"?height="16"?border="0"?align="absbottom">下一頁??????????????</s:if>??????????<s:else>??????????<a?href="showEditPlan.action?page=<s:property?value="?mce_href="showEditPlan.action?page=<s:property?value="page+1"></a>">??????????????????????<img?src="../images/aright.gif"?mce_src="images/aright.gif"?width="18"?height="16"?border="0"?align="absbottom">下一頁</a>??????????</s:else>??????????????????????<a?href="showEditPlan.action?page=<s:property?value="?mce_href="showEditPlan.action?page=<s:property?value="totalPage"></a>">最后一頁</a>??????????????</span>??????????????????</p>??????????????</td>??????????</tr>??</table>????<hr?width="850"?align="left"/>????<table??width="100%">?????????<tr?bgcolor="#ffffff">??????????<td??align="center"?>??????????????<input?type="hidden"?value=""?id="pId"?name="pId"/>??????????????<input?type="submit"??value="查看"?style="width:70px"?onClick="return?rCheck()"?/>?????????????<input?type="button"??value="關閉"?style="width:70px"?onClick="cClose()"?/>??????????</td>??????????</tr>??</table>??</form>??????</body>????</html>?? ?
PS:由于沒有把CSS,JS等代碼粘上來可能此實例不能直接使用,但核心代碼以寫了出來(PlanDaoImpl.java)
大家可以參考參考,如果有什么不明白的地方可以留言
總結
以上是生活随笔為你收集整理的Struts2+Hibernate分页显示实例的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。