原创  hibernate分页(卖咖啡) 收藏

1、分页类
  1. package xxxx.web.util;
  2. public class SplitPage {
  3.     private int nowPageNum = 0// 当前页数
  4.     private int maxResults = 0// 每页最大记录数
  5.     private int firstResult = 0// 从第条记录数开始
  6.     
  7.     private int countResult = 0//总记录数
  8.     
  9.     private int totalPage = 0//总页数
  10.     
  11.     private String url = ""//跳转地址
  12.     public int getNowPageNum() {
  13.         return nowPageNum;
  14.     }
  15.     public void setNowPageNum(int nowPageNum) {
  16.         this.nowPageNum = nowPageNum;
  17.     }
  18.     public int getMaxResults() {
  19.         return maxResults;
  20.     }
  21.     public void setMaxResults(int maxResults) {
  22.         this.maxResults = maxResults;
  23.     }
  24.     
  25.     public int getFirstResult() {
  26.         return firstResult;
  27.     }
  28.     public void setFirstResult(int firstResult) {
  29.         this.firstResult = firstResult;
  30.     }
  31.     
  32.     public void setUrl(String url) {
  33.         this.url = url;
  34.     }
  35.     public String getUrl() {
  36.         return this.url;
  37.     }
  38.     
  39.     public int getCountResult() {
  40.         return countResult;
  41.     }
  42.     public void setCountResult(int countResult) {
  43.         this.countResult = countResult;
  44.     }
  45.     
  46.     public int getTotalPage() {
  47.         return totalPage;
  48.     }
  49.     public void setTotalPage(int totalPage) {
  50.         this.totalPage = totalPage;
  51.     }
  52.     
  53.     public String splitPage(SplitPage sp,String programPath) {
  54.         StringBuffer sbStr = new StringBuffer();
  55.         sbStr.append("共"+sp.getTotalPage()+"页 ");
  56.         
  57.         if (sp.getNowPageNum() <= 1) {
  58.             sbStr.append("首页<IMG SRC=\"" + programPath
  59.                     + "/images/splitpage/firstPage.gif\">");//首页
  60.         } else {
  61.             sbStr.append("<A HREF=\"" + sp.getUrl()+ "page=1\">首页"+"<IMG SRC=\"" + programPath
  62.                     + "/images/splitpage/firstPage.gif\" width=\"14\" border=\"0px\" height=\"11\" >"+"</A>");//首页
  63.         }
  64.         if (sp.getNowPageNum() <= 1) {
  65.             sbStr.append("  上一页<IMG SRC=\"" + programPath
  66.                     + "/images/splitpage/prePage.gif\" height=\"11\" border=\"0px\" align=\"absmiddle\">");//上一页
  67.         } else {
  68.             sbStr.append("    <A HREF=\"" + sp.getUrl() + "page=");
  69.             sbStr.append(sp.getNowPageNum() - 1);
  70.             sbStr.append("\">上一页"+"<IMG SRC=\"" + programPath
  71.                     + "/images/splitpage/prePage.gif\" width=\"14\" height=\"11\" border=\"0px\" align=\"absmiddle\">"+"</A>");//上一页
  72.         }
  73.         sbStr.append("  "+this.getSelect());
  74.         sbStr.append("  <IMG SRC=\"" + programPath
  75.                         + "/images/splitpage/go.gif\" align=\"absmiddle\" onclick=\"forward()\" style=\"cursor:hand;\">");
  76.         if (sp.getNowPageNum() >= sp.getTotalPage()) {
  77.             sbStr.append("  下一页<IMG SRC=\"" + programPath
  78.                     + "/images/splitpage/nextPage.gif\" width=\"14\" height=\"11\" border=\"0px\" align=\"absmiddle\">");//下一页
  79.         } else {
  80.             sbStr.append("  <A HREF=\"" + sp.getUrl() + "page=");
  81.             sbStr.append(sp.getNowPageNum() + 1);
  82.             sbStr.append("\">"+"下一页<IMG SRC=\"" + programPath
  83.                     + "/images/splitpage/nextPage.gif\" width=\"14\" height=\"11\" border=\"0px\" align=\"absmiddle\">"+"</A>");//下一页
  84.         }
  85.         if (sp.getNowPageNum() >= sp.getTotalPage()) {
  86.             sbStr.append("  尾页<IMG SRC=\"" + programPath
  87.                     + "/images/splitpage/endPage.gif\" width=\"14\" height=\"11\" border=\"0\" align=\"absmiddle\">");//尾页
  88.         } else {
  89.             sbStr.append("  <A HREF=\"" + sp.getUrl() + "page=");
  90.             sbStr.append(sp.getTotalPage());
  91.             sbStr.append("\">尾页"+"<IMG SRC=\"" + programPath
  92.                     + "/images/splitpage/endPage.gif\" width=\"14\" height=\"11\" border=\"0\" align=\"absmiddle\">"+"</A>");//尾页
  93.         }
  94.         //sbStr.append("<script language=\"javascript\">function forward(){if (!(/^\\d+$/.test(pagebutton.value))){alert('请用数字!')};else if (pagebutton.value >"+this.totalPage+"){alert('该页不存在!');}else{window.location.href='"+sp.getUrl() + "page='"+"+pagebutton.value;}}</script>");
  95.         sbStr.append("<script language=\"javascript\">function forward(){if (!(/^\\d+$/.test(document.getElementById('pagebutton').value))){alert('请用数字!')};else if (document.getElementById('pagebutton').value >"+this.totalPage+"){alert('该页不存在!');}else{window.location.href='"+sp.getUrl() + "page='"+"+document.getElementById('pagebutton').value;}}</script>");
  96.         //sbStr.append("总记录数:" + sp.getCountResult() +"条");
  97.         
  98.         return sbStr.toString();
  99.     }
  100.     
  101.     //生成框
  102.     private String getSelect(){
  103.         String str ="<input type=\"text\" id=\"pagebutton\" value=\""+this.getNowPageNum()+"\" onchange=\"forward()\" class=\"splitInput\">";
  104.         return str;
  105.     }
  106.     
  107. }





2、DAO
  1. public List findAll(Date tdTimeB, Date tdTimeE, String tkPlate,
  2.             String crName, Integer tdStatus, SplitPage sp) {
  3.         try {
  4.             StringBuffer sb = new StringBuffer();
  5.             sb.append("from BlTransportDispatchView b where 1=1 ");
  6.             if (tdTimeB != null && !"".equals(tdTimeB) && tdTimeE != null && !"".equals(tdTimeE)) {
  7.                 sb.append(" and (b.tdTime >= :tdTimeB and b.tdTime <= :tdTimeE)");
  8.             }
  9.             if (tkPlate != null && !"".equals(tkPlate)) {
  10.                 sb.append(" and b.tkPlate like :tkPlate");
  11.             }
  12.             if (crName != null && !"".equals(crName)) {
  13.                 sb.append(" and b.crName like :crName");
  14.             }
  15.             if(tdStatus!=-1){
  16.                 sb.append(" and b.tdStatus = :tdStatus");
  17.             }
  18.             sb.append(" order by b.tdTime,b.crName,b.tkPlate");
  19.             Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
  20.             Query q = session.createQuery(sb.toString());
  21.             Query qc = session.createQuery("select count(*) " + sb.toString());
  22.             if (tdTimeB != null && !"".equals(tdTimeB) && tdTimeE != null && !"".equals(tdTimeE)) {
  23.                 tdTimeB.setHours(0);
  24.                 tdTimeB.setMinutes(0);
  25.                 tdTimeB.setSeconds(0);
  26.                 tdTimeE.setHours(23);
  27.                 tdTimeE.setMinutes(59);
  28.                 tdTimeE.setSeconds(59);
  29.                 q.setTimestamp("tdTimeB", tdTimeB);
  30.                 q.setTimestamp("tdTimeE", tdTimeE);
  31.                 qc.setTimestamp("tdTimeB", tdTimeB);
  32.                 qc.setTimestamp("tdTimeE", tdTimeE);
  33.             }
  34.             if (tkPlate != null && !"".equals(tkPlate)) {
  35.                 q.setString("tkPlate""%" + tkPlate + "%");
  36.                 qc.setString("tkPlate""%" + tkPlate + "%");
  37.             }
  38.             if (crName != null && !"".equals(crName)) {
  39.                 q.setString("crName""%" + crName + "%");
  40.                 qc.setString("crName""%" + crName + "%");
  41.             }
  42.             if(tdStatus!=-1){
  43.                 q.setInteger("tdStatus", tdStatus);
  44.                 qc.setInteger("tdStatus", tdStatus);
  45.             }
  46.             List lc = qc.list();
  47.             Iterator iter = lc.iterator();
  48.             Integer count = 0;
  49.             while (iter.hasNext()) {
  50.                 count = (Integer) iter.next();
  51.             }
  52.             sp.setCountResult(count);// 总记录数
  53.             sp.setTotalPage((count + sp.getMaxResults() - 1) / sp.getMaxResults());// 总页数
  54.             q.setFirstResult(sp.getFirstResult());// 分页开始记录数
  55.             q.setMaxResults(sp.getMaxResults());// 每页最大记录数
  56.             session.flush();
  57.             return q.list();
  58.         } catch (RuntimeException re) {
  59.             throw re;
  60.         }
  61.     }




3、action
  1. public ActionForward toTDListPage(ActionMapping mapping, ActionForm form,
  2.             HttpServletRequest request, HttpServletResponse response)
  3.             throws Exception {
  4.         String forward=request.getParameter("forward");
  5.         Integer tdStatus=TypeConvertUtil.strToInt(request.getParameter("tdStatus"));
  6.         Date tdTimeB=DateUtil.toDate(request.getParameter("tdTimeB"));
  7.         Date tdTimeE=DateUtil.toDate(request.getParameter("tdTimeE"));
  8.         String tkPlate=request.getParameter("tkPlate");
  9.         String crName=request.getParameter("crName");
  10.         //分页设置
  11.         int nowPage=1;//当前页数
  12.         String strPage=request.getParameter("page");
  13.         if(strPage!=null && !strPage.equals("")){
  14.             nowPage=Integer.parseInt(strPage);//当前页数数据类型转换
  15.         }
  16.         int maxResults=15;//每页显示最大记录数
  17.         SplitPage sp=new SplitPage();
  18.         sp.setNowPageNum(nowPage);//设置当前页数
  19.         sp.setMaxResults(maxResults);//设置每页最大记录数
  20.         sp.setFirstResult((nowPage-1)*maxResults);//设置从第条记录数开始
  21.         String path = request.getContextPath()+"/blTransportDispatch.do?action=toTDListPage&forward="+forward;
  22.         if(tdTimeB!=null && !"".equals(tdTimeB)){
  23.             path=path+"&tdTimeB="+request.getParameter("tdTimeB");
  24.         }
  25.         if(tdTimeE!=null && !"".equals(tdTimeE)){
  26.             path=path+"&tdTimeE="+request.getParameter("tdTimeE");
  27.         }
  28.         if(tkPlate!=null && !"".equals(tkPlate)){
  29.             tkPlate=new String(tkPlate.getBytes("iso-8859-1"),"GBK");
  30.             path=path+"&tkPlate="+URLEncoder.encode(tkPlate,"GBK");
  31.         }
  32.         if(crName!=null && !"".equals(crName)){
  33.             crName=new String(crName.getBytes("iso-8859-1"),"GBK");
  34.             path=path+"&crName="+URLEncoder.encode(crName,"GBK");
  35.         }
  36.         path=path+"&";
  37.         sp.setUrl(path);//设置分页路径
  38.         List list=blTransportDispatchService.findAll(tdTimeB, tdTimeE, tkPlate, crName, tdStatus, sp);
  39.         request.setAttribute("sp", sp.splitPage(sp, request.getContextPath()));
  40.         request.setAttribute("list", list);
  41.         request.setAttribute("tdTimeB", request.getParameter("tdTimeB"));
  42.         request.setAttribute("tdTimeE", request.getParameter("tdTimeE"));
  43.         request.setAttribute("tkPlate", tkPlate);
  44.         request.setAttribute("crName", crName);
  45.         return mapping.findForward(forward);
  46.     }





4、jsp
  1. <%@ page language="java" pageEncoding="GBK"%>
  2. <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
  3. <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
  4. <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
  5. <%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
  6. <%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
  7. <%
  8. String path=request.getContextPath();
  9. String sp=(String)request.getAttribute("sp");
  10. %>
  11. <html>
  12. <head>
  13. <title></title>
  14. <link type="text/css" rel="stylesheet" href="<%=path %>/css/public.css" />
  15. <link type="text/css" rel="stylesheet" href="<%=path %>/common/calendar/calendar-green.css" />
  16. <script type="text/javascript" src="<%=path %>/common/calendar/calendar.js"></script>
  17. <script type="text/javascript" src="<%=path %>/common/calendar/calendar-zh.js"></script>
  18. <script type="text/javascript" src="<%=path %>/common/calendar/calendar-setup.js"></script>
  19. <script type="text/javascript" src="<%=path %>/js/sortableTable.js"></script>
  20. <script type="text/javascript" src="<%=path %>/js/public.js"></script>
  21. </head>
  22. <body>
  23. <form method="get" action="<%=path %>/blTransportDispatch.do">
  24.   <input type="hidden" name="action" value="toTDListPage">
  25.   <input type="hidden" name="forward" value="toTDLookListPage">
  26.   <input type="hidden" name="tdStatus" value="1">
  27.   <table class="table_top_search" width="100%" align="center">
  28.     <tr>
  29.       <td class="table_top_search"><img src="<%=path %>/images/title_front.gif" width="15" height="15"> 调度查看 <img src="<%=path %>/images/button/showHideCnd.gif" width="16" height="16" onClick="showDiv('search')" style="cursor:pointer;"> <span id="mes" onClick="showDiv('search')" style="cursor:pointer;">隐藏条件</span></td>
  30.     </tr>
  31.   </table>
  32.   <br>
  33.   <div id="search">
  34.     <table class="table_public" width="100%" align="center">
  35.       <tr>
  36.         <td rowspan="2" align="right" class="td_public">调度日期:</td>
  37.         <td class="td_public">从
  38.           <input type="text" name="tdTimeB" id="tdTimeB" value="${requestScope.tdTimeB }">
  39.           <img src="<%=path %>/common/calendar/images/button.gif" id="tdTimeB_button" style="cursor: pointer; border: 1px solid red;" title="Date selector" onMouseOver="this.style.background='red';" onMouseOut="this.style.background=''" /></td>
  40.         <td align="right" class="td_public">车牌号:</td>
  41.         <td class="td_public"><input type="text" name="tkPlate" id="tkPlate" value="${requestScope.tkPlate }"></td>
  42.         <td rowspan="2" align="center" class="td_public"><input type="submit" class="wsbutton" value="查  询" onMouseOver="this.className='wsbutton_hover'" onMouseOut="this.className='wsbutton'"></td>
  43.       </tr>
  44.       <tr>
  45.         <td class="td_public">到
  46.           <input type="text" name="tdTimeE" id="tdTimeE" value="${requestScope.tdTimeE }">
  47.           <img src="<%=path %>/common/calendar/images/button.gif" id="tdTimeE_button" style="cursor: pointer; border: 1px solid red;" title="Date selector" onMouseOver="this.style.background='red';" onMouseOut="this.style.background=''" /></td>
  48.         <td align="right" class="td_public">承运商:</td>
  49.         <td class="td_public"><input type="text" name="crName" id="crName" value="${requestScope.crName }"></td>
  50.       </tr>
  51.     </table>
  52.   </div>
  53. </form>
  54. <table id="mytable" class="table_public" width="100%" align="center" sort="1">
  55.   <thead>
  56.     <tr>
  57.       <th style="cursor:pointer" sorttype="Number">序号</th>
  58.       <th style="cursor:pointer" sorttype="String">车牌号</th>
  59.       <th style="cursor:pointer" sorttype="String">承运商</th>
  60.       <th style="cursor:pointer" sorttype="Number">货值</th>
  61.       <th style="cursor:pointer" sorttype="Number">数量</th>
  62.       <th style="cursor:pointer" sorttype="Number">重量</th>
  63.       <th style="cursor:pointer" sorttype="Number">体积</th>
  64.       <th style="cursor:pointer" sorttype="Date">调度时间</th>
  65.       <th style="cursor:pointer" sorttype="String">状态</th>
  66.       <th>修改</th>
  67.       <th>删除</th>
  68.     </tr>
  69.   </thead>
  70.   <tbody>
  71.     <c:forEach var="list" items="${requestScope.list}" varStatus="i">
  72.     <tr align="center" <c:if test="${i.index%2==0}">class="off1"</c:if><c:if test="${i.index%2==1}">class="off2"</c:if> onMouseOver="this.className='up'"<c:if test="${i.index%2==0}"> onMouseOut="this.className='off1'"</c:if><c:if test="${i.index%2==1}"> onMouseOut="this.className='off2'"</c:if>>
  73.       <td class="td_public">${i.index+1 }</td>
  74.       <td class="td_public">${list.tkPlate }</td>
  75.       <td class="td_public">${list.crName }</td>
  76.       <td class="td_public">${list.doGoodsValue }</td>
  77.       <td class="td_public">${list.doGoodsNum }</td>
  78.       <td class="td_public">${list.doGoodsWeight }</td>
  79.       <td class="td_public">${list.doGoodsVol }</td>
  80.       <td class="td_public">${list.tdTime }</td>
  81.       <td class="td_public">
  82.         <c:if test="${list.tdStatus==1 }">已调车</c:if>
  83.         <c:if test="${list.tdStatus==2 }">已添合同</c:if>
  84.       </td>
  85.       <td class="td_public"><a href=""><img src="<%=path %>/images/button/edit.gif" width="16" height="16" border="0"></a></td>
  86.       <td class="td_public"><a href="#"><img src="<%=path %>/images/button/delete.gif" width="16" height="16" border="0"></a></td>
  87.     </tr>
  88.     </c:forEach>
  89.   </tbody>
  90.   <tr>
  91.     <td colspan="3"><input type="button" class="wsbutton" value="返  回" onMouseOver="this.className='wsbutton_hover'" onMouseOut="this.className='wsbutton'" onClick="javascript:window.history.go(-1);">
  92.     </td>
  93.     <td colspan="8" height="30" align="right"><%=sp%></td>
  94.   </tr>
  95. </table>
  96. <script type="text/javascript">
  97. Calendar.setup({
  98.       inputField     :    "tdTimeB",
  99.       ifFormat       :    "%Y-%m-%d",
  100.       button         :    "tdTimeB_button",
  101.       align          :    "Tl",
  102.       singleClick    :    true
  103. });
  104. Calendar.setup({
  105.       inputField     :    "tdTimeE",
  106.       ifFormat       :    "%Y-%m-%d",
  107.       button         :    "tdTimeE_button",
  108.       align          :    "Tl",
  109.       singleClick    :    true
  110. });
  111. </script>
  112. </body>
  113. </html>

发表于 @ 2008年10月07日 16:08:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:存储过程分页 (默金) | 新一篇:存储过程分页 (默金)

  • 发表评论
  • 评论内容:
  •  
Copyright © xinxing028
Powered by CSDN Blog