<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 墻頭草 閱讀(1304) 評論(0)  編輯  收藏

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


    網站導航:
     
    人人游戲網 軟件開發網 貨運專家
    主站蜘蛛池模板: 美女被免费网站在线视频免费| 国产免费黄色大片| 久久精品一区二区免费看| 精品人妻系列无码人妻免费视频| 在线观看亚洲网站| 免费在线观看中文字幕| 99久久99久久精品免费观看| 免费看搞黄视频网站| 亚洲剧场午夜在线观看| 亚洲人成人网站在线观看| 免费视频爱爱太爽了| 黄色免费网址大全| 永久免费无码网站在线观看个| 四虎国产精品成人免费久久 | 四虎国产精品免费永久在线| 一级毛片一级毛片免费毛片| 亚洲av永久无码精品天堂久久 | 又粗又硬免费毛片| 亚洲成AV人在线观看网址| 青青草免费在线视频| 免费被黄网站在观看| 免费一看一级毛片全播放| 亚洲一区二区三区国产精品| 天天拍拍天天爽免费视频| 久久精品中文字幕免费| 精品香蕉在线观看免费| 成全视频在线观看免费| 99久久精品免费精品国产| 黄瓜视频影院在线观看免费| 黄网站免费在线观看| 亚洲综合av一区二区三区不卡| 久久久久亚洲精品美女| 亚洲国产精品yw在线观看| 亚洲午夜无码久久久久小说 | 久久综合日韩亚洲精品色| 亚洲精品国产免费| 亚洲欧美日韩国产精品一区| 亚洲an日韩专区在线| 国产精品久久亚洲一区二区| 国产免费久久精品99久久| 99re免费99re在线视频手机版|