<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無線&移動互聯網技術研發

    換位思考·····
    posts - 19, comments - 53, trackbacks - 0, articles - 283
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    java List 分頁應用(二)

    Posted on 2010-01-10 02:22 Gavin.lee 閱讀(613) 評論(0)  編輯  收藏 所屬分類: java SE & EE
    應用類:
    package com.Gavin.tools.util;

    import java.util.*;

    /**
     * 為List分頁
     * 樣式比較簡單,適合WAP使用,直接輸出頁碼到頁面
     
    */


    public class PagerUtil {
        
    private int pageSize = 10;// 每頁大小
        
        
    private int nextPage;// 下一頁
        
        
    private int prePage;// 前一頁
        
        
    private int pageCount;// 總頁數
        
        
    private int currentPage;// 當前頁
        
        
    private int listSize = 0;// 記錄總數
        
        
    private ArrayList alist = new ArrayList();
        
        
    private String url;

        
        
    /**
         * 初始化記錄列表
         * 
    @param it Iterator
         * 
    @return ArrayList
         
    */

        
    public ArrayList getAlist(Iterator it) {
            ArrayList al 
    = new ArrayList();
            
    while (it.hasNext()) {
                al.add(it.next());
            }

            
    return al;
        }


        
    /**
         * 構造方法
         * 
    @param list Collection
         
    */

        
    public PagerUtil(Collection list) {
            alist 
    = this.getAlist(list.iterator());
            listSize 
    = alist.size();
            nextPage 
    = 1;
            prePage 
    = 0;
            pageCount 
    = listSize / pageSize + 1;
            currentPage 
    = 1;
        }


        
    /**
         * 構造方法
         * 
    @param list Collection
         * 
    @param pageSize int
         
    */

        
    public PagerUtil(Collection list, int pageSize) {
            alist 
    = (ArrayList) list;
            
    this.pageSize = pageSize;
            listSize 
    = alist.size();
            nextPage 
    = 1;
            prePage 
    = 0;
            pageCount 
    = listSize / pageSize;
            
    if (listSize % pageSize > 0{
                pageCount 
    = pageCount + 1;
            }

            currentPage 
    = 1;
        }


        
    public PagerUtil(Vector v, int pageSize) {
            
    for (int i = 0; i < v.size(); i++{
                alist.add(v.get(i));
            }

            
    this.pageSize = pageSize;
            listSize 
    = alist.size();
            nextPage 
    = 1;
            prePage 
    = 0;
            pageCount 
    = listSize / pageSize;
            
    if (listSize % pageSize > 0{
                pageCount 
    = pageCount + 1;
            }

            currentPage 
    = 1;
        }


        
    /**
         * 下一頁
         * 
    @return ArrayList
         
    */

        
    public int nextPage() {
            
    int tempInt = 1;
            
    if (currentPage < this.getPageCount() && currentPage >= 1{
                tempInt 
    = currentPage + 1;
            }
     else
                tempInt 
    = 1;
            
    return tempInt;
        }


        
    /**
         * 前一頁
         * 
    @return ArrayList
         
    */

        
    public int prePage() {
            
    int tempInt = 1;

            
    if (currentPage > 1{
                tempInt 
    = currentPage - 1;
            }
     else
                tempInt 
    = this.getPageCount();
            
    return tempInt;
        }


        
    /**
         * 第一頁
         * 
    @return ArrayList
         
    */

        
    public int firstPage() {
            nextPage 
    = 1;
            prePage 
    = this.getPageCount();
            currentPage 
    = nextPage;
            
    return currentPage;
        }


        
    /**
         * 最后一頁
         * 
    @return ArrayList
         
    */

        
    public int endPage() {
            nextPage 
    = this.getPageCount();
            prePage 
    = nextPage - 1;
            currentPage 
    = nextPage;
            
    return currentPage;
        }


        
    /**
         * 根據當前頁得到記錄列表
         * 
    @param currentPage int
         * 
    @return ArrayList
         
    */

        
    public ArrayList getPageList() {
            ArrayList tempList 
    = new ArrayList();
            
    for (int i = (currentPage - 1* pageSize; i < (currentPage - 1)
                    
    * pageSize + pageSize; i++{
                
    if (i >= 0 && i < this.alist.size()) {
                    tempList.add(alist.get(i));
                }
     else
                    
    break;
            }

            
    return tempList;
        }


        
    public String getPageCtrlString() {
            String strCtrl 
    = "";
            
    if (this.currentPage == 1{
                
    //strCtrl = "首頁 ";
            }
     else {
                strCtrl 
    = "<a href='" + url + "?page=1'>首頁</a> ";
            }

            
    if (this.currentPage == 1{
                
    //strCtrl = strCtrl + "上一頁 ";
            }
     else {
                strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + this.prePage() + "'>上頁</a> ";
            }

            
    if (this.currentPage == this.pageCount) {
                
    //strCtrl = strCtrl + "下一頁 ";
            }
     else {
                strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + this.nextPage() + "'>下頁</a> ";
            }

            
    if (this.currentPage == this.pageCount) {
                
    //strCtrl = strCtrl + "末頁 ";
            }
     else {
                strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + this.getPageCount() + "'>末頁</a> ";
            }

            strCtrl 
    = strCtrl + "\n\r";        //換行
    //        strCtrl = strCtrl
    //                + "跳到 <select name='toPage' onChange=\"window.location='" + url
    //                + "?page='+this.options[this.selectedIndex].value;\">";
            if(this.getPageCount() <= 1{
                
            }
     else if(this.getPageCount() <= 7{
                
    for (int i = 1; i <= this.getCurrentPage(); i++{
                    
    if (i == this.getCurrentPage()) {
                        strCtrl 
    = strCtrl + "<b>" + i + "</b> ";
                    }
     else {
                        strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + i + "'>" + i + "</a> ";
                    }

                }

            }
     else if(4 < this.getCurrentPage() && this.getCurrentPage() < (this.getPageCount() - 3)) {
                
    for (int i = this.getCurrentPage() - 3; i <= this.getCurrentPage() + 3; i++{
                    
    if (i == this.getCurrentPage()) {
                        strCtrl 
    = strCtrl + "<b>" + i + "</b> ";
                    }
     else {
                        strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + i + "'>" + i + "</a> ";
                    }

                }

            }
     else if(this.getCurrentPage() <= 4{
                
    for (int i = 1; i <= 7; i++{
                    
    if (i == this.getCurrentPage()) {
                        strCtrl 
    = strCtrl + "<b>" + i + "</b> ";
                    }
     else {
                        strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + i + "'>" + i + "</a> ";
                    }

                }

            }
     else if((this.getPageCount() - 6<= this.getCurrentPage()) {
                
    for (int i = this.getPageCount() - 6; i <= this.getPageCount(); i++{
                    
    if (i == this.getCurrentPage()) {
                        strCtrl 
    = strCtrl + "<b>" + i + "</b> ";
                    }
     else {
                        strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + i + "'>" + i + "</a> ";
                    }

                }

            }
     else {
                
    for (int i = this.getCurrentPage() - 3; i <= this.getCurrentPage() + 3; i++{
                    
    if (i == this.getCurrentPage()) {
                        strCtrl 
    = strCtrl + "<b>" + i + "</b> ";
                    }
     else {
                        strCtrl 
    = strCtrl + "<a href='" + url + "?page=" + i + "'>" + i + "</a> ";
                    }

                }

            }

            
            strCtrl 
    = strCtrl + "\n\r";        //換行
            strCtrl = strCtrl + "當前" + this.getCurrentPage() + "/" + this.getPageCount() + "頁 每頁" 
                
    + this.getPageSize() + "條 共" + this.listSize + "";
            
    return strCtrl;
        }

        
        
    public int getCurrentPage() {
            
    return currentPage;
        }


        
    public int getNextPage() {
            
    return nextPage;
        }


        
    public int getPageCount() {
            
    return pageCount;
        }


        
    public int getPageSize() {
            
    return pageSize;
        }


        
    public int getPrePage() {
            
    return prePage;
        }


        
    public String getUrl() {
            
    return url;
        }


        
    public void setPrePage(int prePage) {
            
    this.prePage = prePage;
        }


        
    public void setPageSize(int pageSize) {
            
    this.pageSize = pageSize;
        }


        
    public void setPageCount(int pageCount) {
            
    this.pageCount = pageCount;
        }


        
    public void setNextPage(int nextPage) {
            
    this.nextPage = nextPage;
        }


        
    public void setCurrentPage(int currentPage) {
            
    if (currentPage > this.getPageCount()) {
                
    this.currentPage = this.getPageCount();
            }
     else if (currentPage < 0{
                
    this.currentPage = 1;
            }
     else {
                
    this.currentPage = currentPage;
            }

        }


        
    public void setUrl(String url) {
            
    this.url = url;
        }

        
    }


    測試類:
    package com.Gavin.tools.util;

    import java.util.ArrayList;

    public class TestPagrUtil {
        
    public static void main(String args[]) {
            ArrayList al 
    = new ArrayList();
            al.add(
    "a");
            al.add(
    "b");
            al.add(
    "c");
            al.add(
    "d");
            al.add(
    "e");
            al.add(
    "f");
            al.add(
    "g");
            al.add(
    "h");
            al.add(
    "i");
            al.add(
    "j");
            al.add(
    "k");
            al.add(
    "l");
            al.add(
    "m");
            al.add(
    "n");

            PagerUtil autopage 
    = new PagerUtil(al,1);
            autopage.setCurrentPage(
    5);
            autopage.setUrl(
    "/page/user/account/isprize.jsp");
            ArrayList currentPageList 
    = autopage.getPageList();        
            String ctrlStr 
    = autopage.getPageCtrlString();
            
            
    for(Object o: currentPageList) {
                System.out.println(o.toString());
            }

            System.out.println(ctrlStr);
        }

    }

    //
    //e
    //<a href='/page/user/account/isprize.jsp?page=1'>首頁</a> <a href='/page/user/account/isprize.jsp?page=4'>上頁</a> <a href='/page/user/account/isprize.jsp?page=6'>下頁</a> <a href='/page/user/account/isprize.jsp?page=14'>末頁</a> 
    //
    //<a href='/page/user/account/isprize.jsp?page=2'>2</a> <a href='/page/user/account/isprize.jsp?page=3'>3</a> <a href='/page/user/account/isprize.jsp?page=4'>4</a> <b>5</b> <a href='/page/user/account/isprize.jsp?page=6'>6</a> <a href='/page/user/account/isprize.jsp?page=7'>7</a> <a href='/page/user/account/isprize.jsp?page=8'>8</a> 
    //
    //當前5/14頁 每頁1條 共14條
    主站蜘蛛池模板: 日韩免费无码一区二区三区| 33333在线亚洲| 亚洲精品无码鲁网中文电影| 亚洲午夜精品久久久久久浪潮| 免费在线观看黄色毛片| 国产精品免费视频播放器| 国产精品酒店视频免费看| 国产一级大片免费看| 亚洲A∨午夜成人片精品网站| 免费成人av电影| 国产亚洲老熟女视频| 国产AV无码专区亚洲AV漫画| 亚洲线精品一区二区三区 | 中文字幕免费在线看线人动作大片 | 国产免费不卡v片在线观看| 日本免费网址大全在线观看| 99久久综合国产精品免费| 成年人在线免费观看| 国产免费观看青青草原网站| 亚洲日本中文字幕一区二区三区| 亚洲日产韩国一二三四区| 亚洲AV永久无码精品| 亚洲嫩草影院在线观看| 亚洲综合国产成人丁香五月激情| 亚洲AV无码片一区二区三区| 一级人做人爰a全过程免费视频| baoyu777永久免费视频| 91精品手机国产免费| 久久WWW免费人成人片| 免费国产人做人视频在线观看| 亚洲伊人久久综合中文成人网| 亚洲av中文无码乱人伦在线r▽ | 日韩一卡2卡3卡4卡新区亚洲 | 不卡精品国产_亚洲人成在线| 亚洲av无码一区二区三区网站 | 亚洲精品二区国产综合野狼 | 又大又硬又爽免费视频| 亚洲中文字幕无码永久在线| 亚洲视频欧洲视频| 亚洲狠狠婷婷综合久久蜜芽| 国产黄色片免费看|