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

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

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

    posts - 241,  comments - 116,  trackbacks - 0
    package cn.com.timekey.drugmonitor.utils.db;

    import java.util.Collections;
    import java.util.List;

    import org.hibernate.criterion.CriteriaSpecification;

    /**
     * 分頁常用的bean類。<br>
     * 里面包含搜索返回的List,查詢條件DetachedCriteria及分頁菜單用到的數據等。
     *
     * @author KennyLee E-mail:kennylee26@gmail.com 2008-10-25
     * @param <T>
     */
    public class PaginationSupport<T> {

        // Default page size
        public static final int PAGESIZE = 20;
        public static final int MENU_SIZE = 7;

        // total Rows
        private int totalRowsAmount;
        private int pageSize = PAGESIZE;
        private int totalPages;
        // current page number
        private int currentPage = 1;
        // next page number
        private int nextPage;
        // previous page number
        private int previousPage;
        // is has next page
        private boolean hasNext;
        // is has previous page
        private boolean hasPrevious;
        // current page start row number
        private int pageStartRow = 0;
        // current page end row number
        private int pageEndRow;
        private String[] pageMenuNum;
        private int menuSize = MENU_SIZE;
        // Pagination values
        @SuppressWarnings("unchecked")
        private List<T> items = Collections.EMPTY_LIST;
        // select detachedCriteria
        private CriteriaSpecification detachedCriteria;

        public PaginationSupport() {
        }

        /**
         * 構造函數。
         *
         * @param totalRows
         *            總行數
         * @param currentPage
         *            當前頁數
         */
        public PaginationSupport(int totalRows, int currentPage) {
            setPaginationSupport(totalRows, currentPage);
        }

        /**
         * general 構造函數。
         *
         * @param totalRows
         *            總行數
         * @param currentPage
         *            當前頁數
         * @param pageSize
         *            每頁顯示數量。
         */
        public PaginationSupport(int totalRows, int currentPage, int pageSize) {
            this.pageSize = pageSize;
            this.setPaginationSupport(totalRows, currentPage);
        }

        /**
         * @param items
         * @param totalCount
         */
        public PaginationSupport(List<T> items, int totalCount) {
            setPageSize(PAGESIZE);
            setItems(items);
            int currentPage = 1;
            this.setPaginationSupport(totalCount, currentPage);
        }

        /**
         * @param items
         * @param totalCount
         * @param startIndex
         */
        public PaginationSupport(List<T> items, int totalCount, int startIndex) {
            setPageSize(PAGESIZE);
            setItems(items);
            int currentPage = (startIndex / pageSize) + 1;
            this.setPaginationSupport(totalCount, currentPage);
        }

        /**
         * @param items
         *            保存的目標List
         * @param totalCount
         *            查找的總行數
         * @param pageSize
         *            每頁顯示的行數
         * @param startIndex
         *            第幾行開始
         *
         */
        public PaginationSupport(List<T> items, int totalCount, int pageSize,
                int startIndex) {
            setPageSize(pageSize);
            setItems(items);

            int currentPage = (startIndex / pageSize) + 1;
            this.setPaginationSupport(totalCount, currentPage);
        }

        /**
         * DetachedCriteria構造PS時的初始化方法。
         *
         * @param items
         *            保存的目標List
         * @param totalCount
         *            查找的總行數
         * @param pageSize
         *            每頁顯示的行數
         * @param startIndex
         *            第幾行開始
         * @param detachedCriteria
         */
        public PaginationSupport(List<T> items, int totalCount, int pageSize,
                int startIndex, CriteriaSpecification detachedCriteria) {
            setPageSize(pageSize);
            setItems(items);
            int currentPage = (startIndex / pageSize) + 1;
            this.setPaginationSupport(totalCount, currentPage);
            setDetachedCriteria(detachedCriteria);
        }

        /**
         * <p>
         * 替換items,重新構造PaginationSupport對象。
         * </p>
         *
         * @param ps
         * @param newItems
         */
        public PaginationSupport(PaginationSupport<?> ps, List<T> newItems) {
            this(newItems, ps.getTotalRowsAmount(), ps.getPageSize(), ps
                    .getPageStartRow());
        }

        public String[] getPageMenuNum() {
            return pageMenuNum;
        }

        public void setPageMenuNum(String[] pageMenuNum) {
            this.pageMenuNum = pageMenuNum;
        }

        public CriteriaSpecification getDetachedCriteria() {
            return detachedCriteria;
        }

        public void setDetachedCriteria(CriteriaSpecification detachedCriteria) {
            this.detachedCriteria = detachedCriteria;
        }

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

        public void setTotalPages(int totalPages) {
            this.totalPages = totalPages;
        }

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

        public void setPreviousPage(int previousPage) {
            this.previousPage = previousPage;
        }

        public void setHasNext(boolean hasNext) {
            this.hasNext = hasNext;
        }

        public void setHasPrevious(boolean hasPrevious) {
            this.hasPrevious = hasPrevious;
        }

        public void setPageStartRow(int pageStartRow) {
            this.pageStartRow = pageStartRow;
        }

        public List<T> getItems() {
            return items;
        }

        public void setItems(List<T> items) {
            this.items = items;
        }

        /**
         * 分頁導航按鈕
         *
         * @param currentPage
         * @return
         */
        private String[] getPageMenuNums(int currentPage) {
            String[] pageNums = null;

            if (totalPages > menuSize) {// 總頁數大于導航顯示的頁數

                pageNums = new String[menuSize];

                int lastMenuNum = totalPages - menuSize + 1;// 最后一列導航欄按鈕

                int beginMumNum = menuSize;

                int x = menuSize - 1;// 導航顯示系數

                if ((currentPage < lastMenuNum) && (currentPage > beginMumNum)) {

                    for (int i = 0; i < menuSize; i++) {
                        pageNums[i] = String.valueOf(currentPage + i - x / 2);
                    }

                } else if (currentPage > lastMenuNum) {
                    for (int i = 0; i < menuSize; i++) {
                        pageNums[i] = String.valueOf(lastMenuNum + i);
                    }
                } else if (currentPage == lastMenuNum) {

                    if ((lastMenuNum - x / 2) < 1) {
                        lastMenuNum = x / 2 + 1;
                    }

                    for (int i = 0; i < menuSize; i++) {
                        pageNums[i] = String.valueOf(lastMenuNum + i - x / 2);
                    }

                } else if (currentPage == beginMumNum) {
                    for (int i = 0; i < menuSize; i++) {
                        pageNums[i] = String.valueOf(1 + i + x / 2);
                    }
                } else {
                    for (int i = 0; i < menuSize; i++) {
                        pageNums[i] = String.valueOf(1 + i);
                    }
                }

            } else {// 總頁數小于等于導航顯示的頁數,直接顯示。
                pageNums = new String[totalPages];

                // 分頁數比總頁數少
                for (int i = 0; i < totalPages; i++) {
                    pageNums[i] = String.valueOf(i + 1);
                }
            }
            return pageNums;
        }

        /**
         * 設置總行數。
         *
         * @param rows
         *            總行數。
         */
        private void setTotalRowsAmount(int rows) {

            if (rows < 0) {
                totalRowsAmount = 0;
            } else {
                totalRowsAmount = rows;
            }

            if (totalRowsAmount % pageSize == 0) {
                totalPages = totalRowsAmount / pageSize;
            } else {
                totalPages = totalRowsAmount / pageSize + 1;
            }
        }

        /**
         * 設置當前頁數。
         *
         * @param curPage
         */
        private void setCurrentPage(int curPage) {

            if (curPage <= 0) {
                currentPage = 1;
            } else if (curPage > totalPages) {
                currentPage = totalPages;
            } else {
                currentPage = curPage;
            }

            if (currentPage > 1) {
                hasPrevious = true;
            } else {
                hasPrevious = false;
            }

            if (currentPage == totalPages) {
                hasNext = false;
            } else {
                hasNext = true;
            }

            if (totalPages != 0) {
                nextPage = currentPage + 1;
                previousPage = currentPage - 1;
                // 計算當前頁開始行和結束行
                pageStartRow = (currentPage - 1) * pageSize + 1;
                // 記錄索引從0開始
                pageStartRow -= 1;
                pageEndRow = pageStartRow + pageSize;
            }
        }

        public void setPaginationSupport(int totalRows, int currentPage) {
            // 獲取總頁碼,通過對象總數還是每頁多少行的關系
            setTotalRowsAmount(totalRows);
            setCurrentPage(currentPage);
            String[] pageNums = getPageMenuNums(currentPage);
            this.setPageMenuNum(pageNums);
        }

        public void setPageEndRow(int pageEndRow) {
            this.pageEndRow = pageEndRow;
        }

        public int getCurrentPage() {
            return currentPage;
        }

        public boolean isHasNext() {
            return hasNext;
        }

        public boolean isHasPrevious() {
            return hasPrevious;
        }

        public int getNextPage() {
            return nextPage;
        }

        public int getPageSize() {
            return pageSize;
        }

        public int getPreviousPage() {
            return previousPage;
        }

        public int getTotalPages() {
            return totalPages;
        }

        public int getTotalRowsAmount() {
            return totalRowsAmount;
        }

        public int getPageStartRow() {
            return pageStartRow;
        }

        public int getPageEndRow() {
            return pageEndRow;
        }

        public String description() {
            String description = "Total:" + this.getTotalRowsAmount() + " items "
                    + this.getTotalPages() + " pages,Current page:"
                    + this.currentPage + " Previous " + this.hasPrevious + " Next:"
                    + this.hasNext + " start row:" + this.pageStartRow
                    + " end row:" + this.pageEndRow;
            return description;mybatis代碼生成器避免生成Example類的配置參數
        }

        public void init() {
            // do some initialization work
        }

        public int getMenuSize() {
            return menuSize;
        }

        public void setMenuSize(int menuSize) {
            this.menuSize = menuSize;
        }
    }
    查詢頁面內容后,使用 public PaginationSupport(List items, int totalCount, int startIndex)  構造方法來構造PaginationSupport實例。
    然后我的action對應 PaginationSupport實例名字為 pageController(注意實現getPageController方法)。
    初始化分頁url,因為在很多情況下,分頁是帶查詢參數,所以先把基本的url參數填上。參考如下:
    <s:url value="asrd.do" id="pageUrl" escapeAmp="not">
        <s:param name="beginStrDate" value="%{beginStrDate}"></s:param>
        <s:param name="endStrDate" value="%{endStrDate}"></s:param>
        <s:param name="timeCodeStr" value="%{timeCodeStr}"></s:param>
        <s:param name="preTypeName" value="%{preTypeName}"></s:param>
        <s:param name="firstDepId" value="%{firstDepId}"></s:param>
        <s:param name="secondDepId" value="%{secondDepId}"></s:param>
    </s:url>
    這里的url不能使用action來構造鏈接,因為后面需要添加參數到url中,而用action的話會造成項目名重復的問題。所以退而求次的只能使用value構造鏈接,但就必須填上action的后綴了。
    posted on 2011-06-29 09:48 墻頭草 閱讀(1302) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 日本免费一区二区三区最新| 精品免费tv久久久久久久| 亚洲av色香蕉一区二区三区 | 99久久免费看国产精品| 精品免费视在线观看| 免费无码又爽又刺激网站直播| 亚洲免费一区二区| 在线免费视频你懂的| 中文字幕不卡免费视频| 国产在线播放线91免费| 国产免费拔擦拔擦8X高清在线人| 97国免费在线视频| 色欲A∨无码蜜臀AV免费播 | 无遮挡呻吟娇喘视频免费播放| 老司机午夜在线视频免费| 一级做a爰片性色毛片免费网站| jzzjzz免费观看大片免费| 99精品视频在线观看免费| 一区二区三区福利视频免费观看| 最近2019中文字幕免费大全5| 四虎在线免费视频| 蜜臀91精品国产免费观看| 免费国产在线观看| 一本色道久久综合亚洲精品| 亚洲AV人无码激艳猛片| 亚洲欧洲日韩综合| 亚洲色大情网站www| 一级毛片免费不卡| 久久爰www免费人成| a毛片基地免费全部视频| 国产传媒在线观看视频免费观看| 亚洲免费在线观看| 亚洲视频在线观看一区| 亚洲熟妇无码一区二区三区| 午夜成人无码福利免费视频| 久久免费观看国产精品| 大学生美女毛片免费视频| 中文字幕在亚洲第一在线| 亚洲精品午夜视频| 黄色网址在线免费观看| 久久久久久免费一区二区三区|