通用分页实例

1、分页bean

[html][/html] view plaincopy

  1. import java.io.Serializable;
  2. public class PagerBean implements Serializable
  3. {
  4.     private static final long serialVersionUID = 5523540943779813401L;
  5.     /**
  6.      * 页码
  7.      */
  8.     private int pageIndex = 1;
  9.     /**
  10.      * 每页记录数
  11.      */
  12.     private int pageCount = 0;
  13.     /**
  14.      * 总页数
  15.      */
  16.     private int totalPage = 1;
  17.     /**
  18.      * 总记录数
  19.      */
  20.     private int totalRecordCount = 0;
  21.     public int getPageIndex()
  22.     {
  23.         return pageIndex;
  24.     }
  25.     public void setPageIndex( int pageIndex )
  26.     {
  27.         this.pageIndex = pageIndex;
  28.     }
  29.     public int getPageCount()
  30.     {
  31.         return pageCount;
  32.     }
  33.     public void setPageCount( int pageCount )
  34.     {
  35.         this.pageCount = pageCount;
  36.     }
  37.     public int getTotalPage()
  38.     {
  39.         return totalPage;
  40.     }
  41.     public void setTotalPage( int totalPage )
  42.     {
  43.         this.totalPage = totalPage;
  44.     }
  45.     public int getTotalRecordCount()
  46.     {
  47.         return totalRecordCount;
  48.     }
  49.     public void setTotalRecordCount( int totalRecordCount )
  50.     {
  51.         this.totalRecordCount = totalRecordCount;
  52.     }
  53.     // 每页容量 页码必须大于0,否则抛出IllegalArgumentException
  54.     public static PagerBean getPagerBean( int totalRecordCount, int pageCount,
  55.             int pageIndex )
  56.     {
  57.         if ( pageCount <= 0 )
  58.         {
  59.             throw new IllegalArgumentException(
  60.                     “pageCount can’t be less than zero” );
  61.         }
  62.         int totalPage = totalRecordCount / pageCount
  63.                 + (totalRecordCount % pageCount > 0 ? 1 : 0);
  64.         totalPage = Math.max( 1, totalPage );
  65.         pageIndex = Math.max( 1, Math.min( pageIndex, totalPage ) );
  66.         PagerBean pager = new PagerBean();
  67.         pager.setPageCount( pageCount );
  68.         pager.setPageIndex( pageIndex );
  69.         pager.setTotalPage( totalPage );
  70.         pager.setTotalRecordCount( totalRecordCount );
  71.         return pager;
  72.     }
  73. }

2、javascript 处理

[html][/html] view plaincopy

  1. <script type=”text/javascript”>
  2.      var pageIndex = “<s:property value=’#request.pagerBean.pageIndex’/>”;
  3.      function pageNumCheck(){
  4.          var tmp_v = $(“#txtPageNum”).val();
  5.          if (parseInt(tmp_v) != tmp_v) {
  6.              tmp_v = tmp_v.replace(/\D/g, ”);
  7.              tmp_v = tmp_v.replace(/^0*/g, ”);
  8.              $(“#txtPageNum”).val(tmp_v);
  9.              $(“#txtPageNum”).val(tmp_v);
  10.          }
  11.      }
  12.      function previousPage(){
  13.          var page = parseInt(pageIndex);
  14.          goTo(–page);
  15.      }
  16.      function nextPage(){
  17.          var page = parseInt(pageIndex);
  18.          goTo(++page);
  19.      }
  20.      function firstPage(){
  21.          goTo(1);
  22.      }
  23.      function lastPage(){
  24.          goTo(“<s:property value=’#request.pagerBean.totalPage’ />”);
  25.      }
  26.      function pageTo(){
  27.          var pageNum = $(“#txtPageNum”).val();
  28.          var pageIndex = $(“#pageIndex”).text();
  29.          if (pageNum === pageIndex) {
  30.              goTo(pageIndex);
  31.          }
  32.          if (checkNum(pageNum)) {
  33.              var INT_MAX_VALUE = 2147483647;
  34.              var pageIndex = parseInt(pageNum);
  35.              if (pageIndex – INT_MAX_VALUE > 0 || pageIndex – INT_MAX_VALUE < -2147483647)
  36.                  pageIndex = INT_MAX_VALUE;
  37.              goTo(pageIndex);
  38.          }
  39.          else {
  40.              goTo(1);
  41.              $(“#txtPageNum”).val(“”);
  42.              $(“#txtPageNum”).focus();
  43.          }
  44.      }
  45.      function checkNum(numStr){
  46.          var numReg = /^[1-9][0-9]*$/;
  47.          return numReg.test(numStr);
  48.      }
  49.      function goTo(page)
  50.      {
  51.          var command = $(“#selYear”).val();
  52.          window.location.href = “announcementAction!queryWhatNewList.action?command=”+command+”&pageIndex=”+page;
  53.      }
  54. </script>

3、jsp 页面样式

[html][/html] view plaincopy

  1. <div id=”pagediv” style=”float:right;”>
  2.                <table class=”pagerTab” style=”float:right;width:auto;height:32px;”>
  3.                    <tr>
  4.                        <td style=”padding-right:0px;vertical-align:bottom; text-align:right;width: 80px;”>
  5.                            <s:if test=”#request.pagerBean.pageIndex>1″>
  6.                                <img class=”pagination” src=”comm/img/first.gif” onclick=’firstPage();’
  7.                                  title=”<s:text name=’Pager.first’/>” />
  8.                                <img class=”pagination” src=”comm/img/previous.gif” onclick=’previousPage();’
  9.                                  title=”<s:text name=’Pager.previous’/>” />
  10.                            </s:if>
  11.                            <s:else>
  12.                                <img src=”comm/img/first_disabled.gif” title=”<s:text name=’Pager.first’/>” />
  13.                                <img src=”comm/img/previous_disabled.gif” title=”<s:text name=’Pager.previous’/>” />
  14.                            </s:else>
  15.                            <s:if test=”#request.pagerBean.pageIndex==#request.pagerBean.totalPage”>
  16.                                <img src=”comm/img/next_disabled.gif” title=”<s:text name=’Pager.next’/>” />
  17.                                <img src=”comm/img/last_disabled.gif” title=”<s:text name=’Pager.last’/>” />
  18.                            </s:if>
  19.                            <s:else>
  20.                                <img class=”pagination” src=”comm/img/next.gif” onclick=’nextPage();’ title=”<s:text name=’Pager.next’/>” />
  21.                                <img class=”pagination” src=”comm/img/last.gif” onclick=’lastPage();’ title=”<s:text name=’Pager.last’/>” />
  22.                            </s:else>
  23.                        </td>
  24.                        <td>
  25.                            <s:text name=”Pager.page”/>:
  26.                        </td>
  27.                        <td>
  28.                            <span id=”pageIndex”><s:property value=”#request.pagerBean.pageIndex”/></span>/
  29.                         <span id=”totalPage”><s:property value=”#request.pagerBean.totalPage”/></span>
  30.                        </td>
  31.                        <td>
  32.                        </td>
  33.                        <td>
  34.                            <input id=”txtPageNum” type=”text” class=”paging_list_input” onkeyup=”pageNumCheck()”
  35.                         style=”border: #ccc 1px solid;”/>
  36.                        </td>
  37.                        <td>
  38.                            <div style=”background: url(comm/skin/images/paging_list_bg.png) no-repeat;width: 28px;height: 28px;
  39.                             overflow: hidden;line-height: 28px;text-align: center;float: left;margin-left: 6px;”>
  40.                                <a id=”btnGotoPageNum” href=”javascript:void(0)” onclick=”pageTo()”><s:text name=’Pager.goto’/></a>
  41.                            </div>
  42.                        </td>
  43.                    </tr>
  44.                </table>
  45.            </div>

jsp 页面样式

[html][/html] view plaincopy

  1. <div id=”pagediv” style=”float:right;”>
  2.                <table class=”pagerTab” style=”float:right;width:auto;height:32px;”>
  3.                    <tr>
  4.                        <td style=”padding-right:0px;vertical-align:bottom; text-align:right;width: 80px;”>
  5.                            <s:if test=”#request.pagerBean.pageIndex>1″>
  6.             <span onclick=’firstPage();’ ><s:text name=’Pager.first’/></span/>
  7.             <span onclick=’previousPage();’ ><s:text name=’Pager.previous’/></span/>
  8.                            </s:if>
  9.                            <s:else>
  10.             <span><s:text name=’Pager.first’/></span/>
  11.             <span><s:text name=’Pager.previous’/></span/>
  12.                            </s:else>
  13.                            <s:if test=”#request.pagerBean.pageIndex==#request.pagerBean.totalPage”>
  14.             <span onclick=’nextPage();’ ><s:text name=’Pager.next’/></span/>
  15.             <span onclick=’lastPage();’ ><s:text name=’Pager.last’/></span/>
  16.                            </s:if>
  17.                            <s:else>
  18.             <span><s:text name=’Pager.next’/></span/>
  19.             <span><s:text name=’Pager.last’/></span/>
  20.                            </s:else>
  21.                        </td>
  22.                        <td>
  23.                            <s:text name=”Pager.page”/>:
  24.                        </td>
  25.                        <td>
  26.                            <span id=”pageIndex”><s:property value=”#request.pagerBean.pageIndex”/></span>
  27.         /
  28.         <span id=”totalPage”><s:property value=”#request.pagerBean.totalPage”/></span>
  29.                        </td>
  30.                        <td>
  31.                        </td>
  32.                        <td>
  33.                            <input id=”txtPageNum” type=”text” onkeyup=”pageNumCheck()”style=”border: #ccc 1px solid;”/>
  34.                        </td>
  35.                        <td>
  36.                            <div>
  37.                                <a id=”btnGotoPageNum” href=”javascript:void(0)” onclick=”pageTo()”><s:text name=’Pager.goto’/></a>
  38.                            </div>
  39.                        </td>
  40.                    </tr>
  41.                </table>
  42.            </div>

action处理

只获取当前页  和  页的大小。

分页bean保存到request对象中。

标签