动态分页类: Cls_page.java

 package pagination;

 public class Cls_page {
private int nums;// 总条目数
private int current_page;// 当前被选中的页码
private int sub_pages;// 每次显示的页数
private int pageNums;// 总页数
private int[] page_array;// 用来构造分页的数组
private String subPage_link;// 每个分页的链接 /**
*
* Cls_page是的构造函数,用来在创建类的时候自动运行.
*
* @disNums 列表记录显示条数
* @nums 总条目数
* @current_page 当前被选中的页
* @sub_pages 每次显示的页数
* @subPage_link 每个分页的链接
*
* 函数subPageCss(): 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10
* [下页] [尾页]
*/ public Cls_page(int disNums, int nums, int current_page, int sub_pages,
String subPage_link) {
this.nums = nums;
if (current_page == 0) {
this.current_page = 1;
} else {
this.current_page = current_page;
}
this.sub_pages = sub_pages;
this.page_array = new int[this.sub_pages];
int nums2 = nums;
int disNums2 = disNums;
this.pageNums = (nums2 / disNums2);
this.subPage_link = subPage_link; } /*
* 用来给建立分页的数组初始化的函数。
*/
public int[] initarray() {
for (int i = 0; i < this.sub_pages; i++) {
this.page_array[i] = i;
}
return this.page_array;
} /*
* construct_num_Page该函数使用来构造显示的条目 即使:[1][2][3][4][5][6][7][8][9][10]
*/
public int[] construct_num_page() {
int[] current_array;
if (this.pageNums < this.sub_pages) {
current_array = new int[this.pageNums];
for (int i = 0; i < this.pageNums; i++) {
current_array[i] = i + 1;
}
} else {
current_array = this.initarray(); if (this.current_page <= 3) {
for (int i = 0; i < current_array.length; i++) {
current_array[i] = i + 1;
}
} else if (this.current_page <= this.pageNums
&& this.current_page > this.pageNums - this.sub_pages + 1) {
for (int i = 0; i < current_array.length; i++) {
current_array[i] = (this.pageNums) - (this.sub_pages) + 1
+ i;
} } else {
for (int i = 0; i < current_array.length; i++) {
current_array[i] = this.current_page - 2 + i; }
} } return current_array;
} /*
* 构造分页 当前第1/453页 [首页] [上页] 1 2 3 4 5 6 7 8 9 10 [下页] [尾页]
*/ public String subPageCss() {
String subPageCssStr = "";
subPageCssStr += "当前第" + this.current_page + "/" + this.pageNums + "页 ";
if (this.current_page > 1) {
String firstPageUrl = this.subPage_link + "1";
String prewPageUrl = this.subPage_link + (this.current_page - 1);
subPageCssStr += "[<a href='" + firstPageUrl + "'>首页</a>] ";
subPageCssStr += "[<a href='" + prewPageUrl + "'>上一页</a>] ";
} else {
subPageCssStr += "[首页] ";
subPageCssStr += "[上一页] ";
} int[] a = this.construct_num_page(); for (int i = 0; i < a.length; i++) {
int s = a[i];
if (s == this.current_page) {
subPageCssStr += "[<span style='color:red;font-weight:bold;'>"
+ s + "</span>]";
} else {
String url = this.subPage_link + s;
subPageCssStr += "[<a href='" + url + "'>" + s + "</a>]"; }
} if (this.current_page < this.pageNums) {
String lastPageUrl = this.subPage_link + this.pageNums;
String nextPageUrl = this.subPage_link + (this.current_page + 1);
subPageCssStr += " [<a href='" + nextPageUrl + "'>下一页</a>] ";
subPageCssStr += "[<a href='" + lastPageUrl + "'>尾页</a>] ";
} else {
subPageCssStr += "[下一页] ";
subPageCssStr += "[尾页] ";
}
subPageCssStr += " 共" + this.nums + "条记录"; return subPageCssStr;
} }

 调用页面:index.jsp

 <%@ page language="java" import="java.util.*,pagination.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head> <body> JAVA动态分页类调用<hr/>
<%
String p = request.getParameter("p") == null ? "1" : request.getParameter("p");//获取页码
int pagenum = Integer.parseInt(p);//转为int类型
int limit = 10;//显示条数
int start = (pagenum - 1) * limit;//开始位置
int nums = 400;//总条目数 //输出测试
for (int i = start; i < start + limit; i++) {
out.print(i + 1 + ".................................................<br/>");
} //调用分页类 new Cls_page(列表记录显示条数, 记录总条数, 当前被选中的页码, 页码链接数,每个分页的路径)
Cls_page cls = new Cls_page(limit, nums, pagenum, 10,"index.jsp?p=");
out.print(cls.subPageCss());
%> </body>
</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条记录

05-02 03:02