關于分頁經(jīng)驗不是很多,讓我來做分頁,我習慣用這樣的一種方法,以前用ASP和PHP的時候也是這樣來處理的。也沒怎么認真去檢查是否科學,不過先做出來再說,交出一個不怎么好的作品總比交不出作品好。不好的話還可以去完善和優(yōu)化嘛。

          首先,先創(chuàng)建一個顯示每一頁(有若干條數(shù)據(jù))的方法(如下面的hfindAll方法),決定好每一頁接收一個頁碼參數(shù),然后顯示該頁碼里的數(shù)據(jù)。比如有100條數(shù)據(jù),那么分10頁,那第幾頁顯示第幾條至第幾條數(shù)據(jù)。然后再創(chuàng)建一個方法是用于在view層顯示頁數(shù)(如下面的amountPage方法),最后是在view層上為每一頁數(shù)的數(shù)字加上超鏈接。

          以下是主要代碼:
          public class LoginDAO extends HibernateDaoSupport {
          private int PageSize =5;
          /*設置每頁的數(shù)據(jù)條數(shù)*/

           public Integer amountPage(){
               Session session =this.getSession();
            Query query = session.createQuery("from Login");
            query.setCacheable(true);
            int a = query.list().size()%PageSize;
          /*總記錄數(shù)/每頁數(shù)據(jù)數(shù),判斷是否能整除*/
            Integer amount;
            if(a!=0){
             amount = query.list().size()/PageSize+1;
          /*如果整除有余數(shù),則頁數(shù)加1*/
            }else{
             amount = query.list().size()/PageSize;
          /*如果整除沒余數(shù),則直接總記錄數(shù)/每頁數(shù)據(jù)數(shù)*/
            }
            return amount;
           }

           public List hfindAll(String pagenum) {
               Session session =this.getSession();
            Query query = session.createQuery("from Login");
            if (pagenum == null){
          /*如果pagenum是空,則數(shù)據(jù)從第一條開始*/
            query.setFirstResult(0);
          /*設置查詢開始的第幾條數(shù)據(jù),這里是從第1條開始*/
            query.setMaxResults(PageSize);
          /*設置查詢數(shù)據(jù)條數(shù),這里是5條*/
            query.setCacheable(true);
          /*設置一級緩存*/
            }else{
             Integer p = (Integer.valueOf(pagenum)-1) * PageSize;
             query.setFirstResult(p);
             query.setMaxResults(PageSize);
             query.setCacheable(true);
            }
            return query.list();
           }
          }

          然后需要解決的是一些數(shù)據(jù)傳遞,類型轉換和在view層顯示的問題,主要代碼如下:

          在ACTION里:
          String strpagenum = request.getParameter("pagenum");
            List results= loginManage.hfind(strpagenum);
            Integer amountPage = loginManage.amountPage();
              if(results!=null){
                       HttpSession session=request.getSession();
                       session.setAttribute("results",results);
                       session.setAttribute("amountPage",amountPage);
                       return mapping.findForward("ok");
                   }
              return mapping.findForward("fail");

          在view視圖里:
          顯示每頁數(shù)據(jù)的代碼:

           <logic:iterate id="element" name="results">    <tr>
             <td width="100"><input type="checkbox" name="select" value="<bean:write name="element" property="id"/>"><bean:write name="element" property="id"/></td>
             <td width="100"><bean:write name="element" property="name"/> </td>
             <td width="100"><bean:write name="element" property="password"/></td>
             </tr></logic:iterate>

          顯示頁碼的代碼:
          <%
             int i;
            int a=Integer.parseInt(session.getAttribute("amountPage").toString());
             for (i=1;i<=a;i++){
             out.println("<tr>");  
                    out.println("<td><a href='display.do?pagenum="+ i +"'>"+ i +"</a></td>");    
                    out.println("</tr>");    
             }
              %>


          posted on 2008-01-10 18:55 lzj520 閱讀(2949) 評論(6)  編輯  收藏 所屬分類: Struts1Spring個人學習日記Hibernate
          主站蜘蛛池模板: 孟州市| 普陀区| 会泽县| 营山县| 罗山县| 岑溪市| 江达县| 黄平县| 普兰县| 来宾市| 齐河县| 固阳县| 宾川县| 大冶市| 牙克石市| 镇江市| 名山县| 嘉善县| 贞丰县| 山西省| 平武县| 旬阳县| 辽阳市| 洛宁县| 云浮市| 弥勒县| 台湾省| 贡山| 本溪市| 云安县| 潼南县| 冷水江市| 博罗县| 阳东县| 将乐县| 阜新市| 江达县| 合肥市| 奉化市| 宜兰县| 乐至县|