迷失北京

          BlogJava 聯系 聚合 管理
            60 Posts :: 0 Stories :: 13 Comments :: 0 Trackbacks

          以前博客上轉載過一個師哥的博客,是關于SSH分頁的,關于分頁的實現大家都有自己用的最順手的方法,但是其實分頁的思想是相似的。感覺師兄的那個方法太“重”了。其實一個分頁的工具沒必要再細分成那么多層,這樣顯得太笨重了。下面推薦一個輕量級的方法,小巧易用,不管是純粹jsp,還是SSH,均可以使用。廢話不再多說上源碼:

          Pager類(關于分頁的實體類):

          public class Pager {
          private int totalRows; // 總行數
          private int pageSize = 15; // 每頁顯示的行數
          private int currentPage; // 當前頁號
          private int totalPages; // 總頁數
          private int startRow; // 當前頁在數據庫中的起始行
          public Pager() {
          }
          public Pager(int _totalRows) {
          totalRows
          = _totalRows;
          totalPages
          = totalRows / pageSize;
          int mod = totalRows % pageSize;
          if (mod > 0) {
          totalPages
          ++;
          }
          currentPage
          = 1;
          startRow
          = 0;
          }
          public int getStartRow() {
          return startRow;
          }
          public int getTotalPages() {
          return totalPages;
          }
          public int getCurrentPage() {
          return currentPage;
          }
          public int getPageSize() {
          return pageSize;
          }
          public void setTotalRows(int totalRows) {
          this.totalRows = totalRows;
          }
          public void setStartRow(int startRow) {
          this.startRow = startRow;
          }
          public void setTotalPages(int totalPages) {
          this.totalPages = totalPages;
          }
          public void setCurrentPage(int currentPage) {
          this.currentPage = currentPage;
          }
          public void setPageSize(int pageSize) {
          this.pageSize = pageSize;
          }
          public int getTotalRows() {
          return totalRows;
          }
          public void first() {
          currentPage
          = 1;
          startRow
          = 0;
          }
          public void previous() {
          if (currentPage == 1) {
          return;
          }
          currentPage
          --;
          startRow
          = (currentPage - 1) * pageSize;
          }
          public void next() {
          if (currentPage < totalPages) {
          currentPage
          ++;
          }
          startRow
          = (currentPage - 1) * pageSize;
          }
          public void last() {
          currentPage
          = totalPages;
          startRow
          = (currentPage - 1) * pageSize;
          }
          public void refresh(int _currentPage) {
          currentPage
          = _currentPage;
          if (currentPage > totalPages) {
          last();
          }
          }
          }


          PagerService類(掌控分頁邏輯的類):

          public class PagerService {
          public Pager getPager(String currentPage, String pagerMethod, int totalRows) {
          // 定義pager對象,用于傳到頁面
          Pager pager = new Pager(totalRows);
          // 如果當前頁號為空,表示為首次查詢該頁
          // 如果不為空,則刷新pager對象,輸入當前頁號等信息
          if (currentPage != null) {
          pager.refresh(Integer.parseInt(currentPage));
          }
          // 獲取當前執行的方法,首頁,前一頁,后一頁,尾頁。
          if (pagerMethod != null) {
          if (pagerMethod.equals("first")) {
          pager.first();
          }
          else if (pagerMethod.equals("previous")) {
          pager.previous();
          }
          else if (pagerMethod.equals("next")) {
          pager.next();
          }
          else if (pagerMethod.equals("last")) {
          pager.last();
          }
          }
          return pager;
          }
          }


          業務邏輯中的具體使用方法:

          public String pagerMessages() {
          // 獲得所有的帖子總數
          msgNum = messageService.getRows();
          // 獲得制定方法的pager對象
          pager = pagerService.getPager(this.getCurrentPage(), this.getPagerMethod(), msgNum);
          // 當前頁面
          this.setCurrentPage(String.valueOf(pager.getCurrentPage()));
          // 總條數
          this.setMsgNum(this.getMsgNum());
          // 用于分頁顯示的記錄
          themeMessages = messageService.getMessages(pager.getPageSize(), pager.getStartRow());

          return SUCCESS;
          }


          怎么樣是不是感覺清爽了很多,其實這不是我的思想成果,只是拿別人的知識成果來和大家分享一下!共同進步.....

          posted on 2011-04-12 10:52 王康 閱讀(95) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 深州市| 文水县| 云安县| 桐乡市| 龙海市| 古田县| 防城港市| 溧水县| 乐陵市| 林州市| 宣汉县| 兴和县| 东宁县| 长葛市| 潜山县| 乌兰察布市| 衡山县| 安远县| 永泰县| 蛟河市| 北川| 大竹县| 怀宁县| 台湾省| 屏东县| 鹤峰县| 黄山市| 朝阳区| 长垣县| 景谷| 建宁县| 昌都县| 准格尔旗| 沂源县| 松潘县| 花莲县| 沁水县| 广德县| 滕州市| 黄山市| 瑞安市|