Java 动态分页类

 

 动态分页类: Cls_page.java

  1 package pagination;
  2 
  3 public class Cls_page {
  4     private int nums;// 总条目数
  5     private int current_page;// 当前被选中的页码
  6     private int sub_pages;// 每次显示的页数
  7     private int pageNums;// 总页数
  8     private int[] page_array;// 用来构造分页的数组
  9     private String subPage_link;// 每个分页的链接
 10 
 11     /**
 12      * 
 13      * Cls_page是的构造函数,用来在创建类的时候自动运行.
 14      * 
 15      * @disNums 列表记录显示条数
 16      * @nums 总条目数
 17      * @current_page 当前被选中的页
 18      * @sub_pages 每次显示的页数
 19      * @subPage_link 每个分页的链接
 20      * 
 21      *               函数subPageCss(): 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10
 22      *               [下页] [尾页]
 23      */
 24 
 25     public Cls_page(int disNums, int nums, int current_page, int sub_pages,
 26             String subPage_link) {
 27         this.nums = nums;
 28         if (current_page == 0) {
 29             this.current_page = 1;
 30         } else {
 31             this.current_page = current_page;
 32         }
 33         this.sub_pages = sub_pages;
 34         this.page_array = new int[this.sub_pages];
 35         int nums2 = nums;
 36         int disNums2 = disNums;
 37         this.pageNums = (nums2 / disNums2);
 38         this.subPage_link = subPage_link;
 39 
 40     }
 41 
 42     /*
 43      * 用来给建立分页的数组初始化的函数。
 44      */
 45     public int[] initarray() {
 46         for (int i = 0; i < this.sub_pages; i++) {
 47             this.page_array[i] = i;
 48         }
 49         return this.page_array;
 50     }
 51 
 52     /*
 53      * construct_num_Page该函数使用来构造显示的条目 即使:[1][2][3][4][5][6][7][8][9][10]
 54      */
 55     public int[] construct_num_page() {
 56         int[] current_array;
 57         if (this.pageNums < this.sub_pages) {
 58             current_array = new int[this.pageNums];
 59             for (int i = 0; i < this.pageNums; i++) {
 60                 current_array[i] = i + 1;
 61             }
 62         } else {
 63             current_array = this.initarray();
 64 
 65             if (this.current_page <= 3) {
 66                 for (int i = 0; i < current_array.length; i++) {
 67                     current_array[i] = i + 1;
 68                 }
 69             } else if (this.current_page <= this.pageNums
 70                     && this.current_page > this.pageNums - this.sub_pages + 1) {
 71                 for (int i = 0; i < current_array.length; i++) {
 72                     current_array[i] = (this.pageNums) - (this.sub_pages) + 1
 73                             + i;
 74                 }
 75 
 76             } else {
 77                 for (int i = 0; i < current_array.length; i++) {
 78                     current_array[i] = this.current_page - 2 + i;
 79 
 80                 }
 81             }
 82 
 83         }
 84 
 85         return current_array;
 86     }
 87 
 88     /*
 89      * 构造分页 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
 90      */
 91 
 92     public String subPageCss() {
 93         String subPageCssStr = "";
 94         subPageCssStr += "当前第" + this.current_page + "/" + this.pageNums + "页 ";
 95         if (this.current_page > 1) {
 96             String firstPageUrl = this.subPage_link + "1";
 97             String prewPageUrl = this.subPage_link + (this.current_page - 1);
 98             subPageCssStr += "[<a href='" + firstPageUrl + "'>首页</a>] ";
 99             subPageCssStr += "[<a href='" + prewPageUrl + "'>上一页</a>] ";
100         } else {
101             subPageCssStr += "[首页] ";
102             subPageCssStr += "[上一页] ";
103         }
104 
105         int[] a = this.construct_num_page();
106 
107         for (int i = 0; i < a.length; i++) {
108             int s = a[i];
109             if (s == this.current_page) {
110                 subPageCssStr += "[<span style='color:red;font-weight:bold;'>"
111                         + s + "</span>]";
112             } else {
113                 String url = this.subPage_link + s;
114                 subPageCssStr += "[<a href='" + url + "'>" + s + "</a>]";
115 
116             }
117         }
118 
119         if (this.current_page < this.pageNums) {
120             String lastPageUrl = this.subPage_link + this.pageNums;
121             String nextPageUrl = this.subPage_link + (this.current_page + 1);
122             subPageCssStr += " [<a href='" + nextPageUrl + "'>下一页</a>] ";
123             subPageCssStr += "[<a href='" + lastPageUrl + "'>尾页</a>] ";
124         } else {
125             subPageCssStr += "[下一页] ";
126             subPageCssStr += "[尾页] ";
127         }
128         subPageCssStr += "  共" + this.nums + "条记录";
129 
130         return subPageCssStr;
131     }
132 
133 }

 

 

 调用页面:index.jsp

 1 <%@ page language="java" import="java.util.*,pagination.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4     <head>
 5         <title>My JSP 'index.jsp' starting page</title>
 6         <meta http-equiv="pragma" content="no-cache">
 7         <meta http-equiv="cache-control" content="no-cache">
 8         <meta http-equiv="expires" content="0">
 9     </head>
10 
11     <body>
12     
13         JAVA动态分页类调用<hr/>
14         <%
15         String p = request.getParameter("p") == null ? "1" : request.getParameter("p");//获取页码
16         int pagenum = Integer.parseInt(p);//转为int类型
17         int limit = 10;//显示条数
18         int start = (pagenum - 1) * limit;//开始位置
19         int nums = 400;//总条目数
20         
21         //输出测试
22         for (int i = start; i < start + limit; i++) {
23         out.print(i + 1 + ".................................................<br/>");
24         }
25             
26         //调用分页类 new Cls_page(列表记录显示条数, 记录总条数, 当前被选中的页码, 页码链接数,每个分页的路径)
27         Cls_page cls = new Cls_page(limit, nums, pagenum, 10,"index.jsp?p=");
28         out.print(cls.subPageCss());
29         %>
30         
31     </body>
32 </html>

运行结果:

JAVA动态分页类调用


31.................................................
32.................................................
33.................................................
34.................................................
35.................................................
36.................................................
37.................................................
38.................................................
39.................................................
40.................................................
当前第4/40页 [首页] [上一页] [2][3][4][5][6][7][8][9][10][11] [下一页] [尾页] 共400条记录

posted @ 2014-03-12 11:14  hlb  阅读(498)  评论(0编辑  收藏  举报