兩畝三分地

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            17 隨筆 :: 20 文章 :: 2 評論 :: 0 Trackbacks
          對于表格數據處理的話,displayTag有很大的優勢;但是有些時候還得自己動手做點工作;
          我自己寫了一個PageTag的類來處理一些簡單的工作。
          PageTag.java
            1 package com.blog.utils;
            2 
            3 /**
            4  *
            5  * @author Chucky
            6  */
            7 public class PageTag {
            8     /* pageNo:  the page number of current page 
            9      * pageSize: how many records in one page
           10      * totalPages: the number of totalpages
           11      */
           12 
           13     private int pageNo;
           14     private int pageSize;
           15     private int totalPages;
           16 
           17     public PageTag(int pageNo, int pageSize) {
           18         this.pageNo = pageNo;
           19         this.pageSize = pageSize;
           20         this.totalPages = 1;
           21     }
           22 
           23     //accroding to the number of records to calculate the totalpages
           24     public PageTag(int pageNo, int pageSize, int records) {
           25         this.pageNo = pageNo;
           26         this.pageSize = pageSize;
           27         this.totalPages = countPages(records);
           28     }
           29 
           30     public int getPageNo() {
           31         return pageNo;
           32     }
           33 
           34     public void setPageNo(int pageNo) {
           35         this.pageNo = pageNo;
           36     }
           37 
           38     public int getPageSize() {
           39         return pageSize;
           40     }
           41 
           42     public void setPageSize(int pageSize) {
           43         this.pageSize = pageSize;
           44     }
           45 
           46     public int getTotalPages() {
           47         return totalPages;
           48     }
           49 
           50     public void setTotalPages(int totalPages) {
           51         this.totalPages = totalPages;
           52     }
           53 
           54     public int countPages(int records) {
           55         totalPages = records % pageSize == 0 ? records / pageSize : records / pageSize + 1;
           56         return totalPages;
           57     }
           58 
           59     /* getStartPos: get the start postion of the records
           60      */
           61     public int getStartPos() {
           62         int startPos = (pageNo - 1* pageSize;
           63         return startPos;
           64     }
           65 
           66     /* get the page number of previos page
           67      */
           68     public int getPrePage() {
           69         int prePage = pageNo - 1;
           70         if (prePage == 0) {
           71             prePage = pageNo;
           72         }
           73         return prePage;
           74     }
           75 
           76     /* get the page number of next page
           77      */
           78     public int getNextPage() {
           79         int nextPage = pageNo + 1;
           80         if (nextPage > totalPages) {
           81             nextPage = totalPages;
           82         }
           83         return nextPage;
           84     }
           85 
           86     /* get the page number of previos page which specified
           87      */
           88     public int getPrePage(int page) {
           89         int prePage = page - 1;
           90         if (prePage == 0) {
           91             prePage = page;
           92         }
           93         return prePage;
           94     }
           95 
           96     /* get the page number of next page which specified
           97      */
           98     public int getNextPage(int page) {
           99         int nextPage = page + 1;
          100         if (nextPage > totalPages) {
          101             nextPage = totalPages;
          102         }
          103         return nextPage;
          104     }
          105 }
          這個類寫的很簡單,注釋也很清楚;還是讓我們結合項目來看看如何使用。
           1          List blogs = null;
           2 
           3 
           4         /* pages proceesor:
           5          * get related the number of records (such as blogs number);
           6          * then calculate totalpages;
           7          * store pageTag to request if nesscessary.
           8          */
           9         String page = request.getParameter("page");
          10         int pageSize = 10;
          11         int pageNo = 1;
          12         if (page != null) {
          13             pageNo = new Integer(page);
          14         }
          15         PageTag pageTag = new PageTag(pageNo, pageSize);
          16 
          17         if (cid == null) {
          18             sql = "select b.id as id from blog b, category c where category_id = c.id";
          19         } else {
          20             sql = "select b.id as id from blog b, category c where category_id = " + cid + " and category_id = c.id";
          21         }
          22         try {
          23             blogs = (List) qr.query(sql, new BeanListHandler(Blog.class));
          24             if (blogs.size() > 0) {
          25                 pageTag.countPages(blogs.size());
          26             }
          27         } catch (SQLException ex) {
          28             Logger.getLogger(HomeServlet.class.getName()).log(Level.SEVERE, null, ex);
          29         }
          30         request.setAttribute("pageTag", pageTag);
          31 
          32 
          33         /* blog collection
          34          * if cid not equals null which means it queries all blogs that belong to specified category
          35          * otherwise get all blogs
          36          */
          37 
          38         if (cid == null) {
          39             //sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = c.id order by date desc";
          40             sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
          41                     "(select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
                       group by blog.id) as b, category c 
          " +
          42                     "where categoryId = c.id " +
          43                     "order by date desc " +
          44                     "limit " + pageTag.getStartPos() + ","+pageTag.getPageSize();
          45         } else {
          46             sql = "select id,name from category where id = " + cid;
          47             try {
          48                 List list = (List) qr.query(sql, new BeanListHandler(Category.class));
          49                 Category category = (Category) list.get(0);
          50                 request.setAttribute("category", category);
          51             } catch (SQLException ex) {
          52                 Logger.getLogger(HomeServlet.class.getName()).log(Level.SEVERE, null, ex);
          53             }
          54             //sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = " + cid + " and category_id = c.id order by date desc";
          55             sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
          56                     "(select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
                                  group by blog.id) as b, category c 
          " +
          57                     "where categoryId = " + cid + " and categoryId = c.id " +
          58                     "order by date desc " +
          59                     "limit " + pageTag.getStartPos() + ","+pageTag.getPageSize();
          60         }
          61         try {
          62             blogs = (List) qr.query(sql, new BeanListHandler(Blog.class));
          63         } catch (SQLException ex) {
          64             Logger.getLogger(HomeServlet.class.getName()).log(Level.SEVERE, null, ex);
          65         }
          66         request.setAttribute("blogs", blogs);
          我們可以看到4-30行是有關pageTag的使用;首先從request取得當前頁(page屬性),接著創建pageTag對象,根據需求(cid)取得blog的數量;從而得到總頁數;最后將pageTag對象保存至request中。
          隨后在查詢blog的時候,我們再次使用pageTag.getStartPos()和pageTag.getPageSize()這兩個方法,取得查詢數據開始的位置與長度。Job Done!

          再看jsp頁面
           1 <div id="pagetags">
           2             <% if (pageTag.getTotalPages() != 0){
           3                 int totalPages = pageTag.getTotalPages();
           4                 if (totalPages > 1) {%>
           5                 <span class="pagelinks">[<href="<%=request.getContextPath()%>/HomeServlet?page=1">首頁</a>/<href="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getPrePage()%>">上一頁</a>]
           6 
           7                     <% for (int i=1;i<=10;i++){
           8                         int base = (pageTag.getPageNo()/10)*10;
           9                         int remainder =pageTag.getPageNo()%10;
          10                         if ((i+base)>pageTag.getTotalPages()){
          11                             break;
          12                         }
          13                         if (i != remainder){
          14                         %>
          15                         <href="<%=request.getContextPath()%>/HomeServlet?page=<%=i+base%>"><%=i+base%></a>
          16                         <%}else{%>
          17                         <strong><%=pageTag.getPageNo()%></strong>
          18                     <%}}%>
          19 
          20                     [<href="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getNextPage()%>">下一頁</a>/<href="<%=request.getContextPath()%>/HomeServlet?page=<%=totalPages%>">尾頁</a>]</span>
          21                 <%}else{%>
          22                 <span class="pagelinks">共1頁</span>
          23             <% }}%>
          24         </div>
          首頁很簡單,page設為1即可;尾頁的話就totalPage的值;這里集中要說一下for循環的內容。
          line 8  取得一個基數base(類似10,20,30);
          line 9  取得余數;
          line 10 如果i加上基數base 大于 總頁數的話,就沒有必要再繼續打印頁碼了;
          line 15 如果i不等于余數的話,
          line 16 為該頁碼添加鏈接;
          line 17 否則,不必為該頁碼添加鏈接。

          因為數據不多,之前的pageSize我改成了1;讓我們看下效果:




          posted on 2009-10-07 18:58 Chucky 閱讀(247) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 南陵县| 曲沃县| 天祝| 霍林郭勒市| 灵川县| 永安市| 江华| 积石山| 建瓯市| 全南县| 盐边县| 都兰县| 乐都县| 合肥市| 双辽市| 津市市| 罗山县| 施秉县| 策勒县| 彝良县| 全椒县| 敦煌市| 北流市| 中牟县| 辽宁省| 浦北县| 长海县| 府谷县| 沂水县| 华坪县| 平定县| 新化县| 辽源市| 镇平县| 汶川县| 永靖县| 英吉沙县| 赞皇县| 尚志市| 淮阳县| 古田县|