①分页效果:
②分页PageBean模型设计
1
package com.cn.utils;
2
3
import java.util.List;
4
5
public class PageBean<T> {
6
7
/**
8
* 供外界调用,只能传入一下参数
9
* @param currentPageNumber 要显示的当前页码
10
* @param totalRecordCounts 总记录条数
11
* @param pageSize 每页显示条数
12
*/
13
public PageBean(int currentPageNumber, int totalRecordCounts, int pageSize){
14
this.currentPageNumber = currentPageNumber;
15
this.totalRecordCounts = totalRecordCounts;
16
this.pageSize = pageSize;
17
18
//计算总页数
19
totalPageCounts = totalRecordCounts%pageSize==0?totalRecordCounts/pageSize:(totalRecordCounts/pageSize+1);
20
21
//计算每页开始的索引值
22
startIndex = (currentPageNumber-1)*pageSize;
23
24
/**
25
* 计算显示的开始页码和显示的结束页码
26
*/
27
/**
28
* 注意设置变量,设置分页显示时显示的最大页码数量,这个数字必须为奇数,为了让当前页居中
29
*/
30
int maxShowPageNum = 5;//可自由设置(为奇数即可)
31
if(totalPageCounts <= maxShowPageNum){//总页数<=maxShowPageNum
32
beginPageNumber = 1;
33
endPageNumber = totalPageCounts;
34
}else{//总页数>=maxShowPageNum
35
beginPageNumber = currentPageNumber-maxShowPageNum/2;//假如maxShowPageNum=5,这是候当前页居中,则左右两边为当前页+maxShowPageNum/2
36
endPageNumber = currentPageNumber+maxShowPageNum/2;
37
if(beginPageNumber < 1){
38
beginPageNumber = 1;
39
endPageNumber = maxShowPageNum;
40
}
41
if(endPageNumber > totalPageCounts){
42
beginPageNumber = totalPageCounts-(maxShowPageNum-1);
43
endPageNumber = totalPageCounts;
44
}
45
}
46
}
47
48
/**
49
* 要显示的当前页码
50
*/
51
private int currentPageNumber;
52
53
/**
54
* 记录总条数
55
*/
56
private int totalRecordCounts;
57
58
/**
59
* 每页显示数据条数
60
*/
61
private int pageSize;
62
63
/**
64
* 总页数
65
*/
66
private int totalPageCounts;
67
68
/**
69
* 每页开始的索引
70
*/
71
private int startIndex;
72
73
/**
74
* 分页查询的数据
75
*/
76
private List<T> pageList;
77
78
/**
79
* 页码显示的起始页
80
*/
81
private int beginPageNumber;
82
83
/**
84
* 页码显示的结束码
85
*/
86
private int endPageNumber;
87
88
/**
89
* 设置分页跳转路径
90
*/
91
private String pageUri;
92
93
public String getPageUri() {
94
return pageUri;
95
}
96
97
public void setPageUri(String pageUri) {
98
this.pageUri = pageUri;
99
}
100
101
public int getBeginPageNumber() {
102
return beginPageNumber;
103
}
104
105
public void setBeginPageNumber(int beginPageNumber) {
106
this.beginPageNumber = beginPageNumber;
107
}
108
109
public int getEndPageNumber() {
110
return endPageNumber;
111
}
112
113
public void setEndPageNumber(int endPageNumber) {
114
this.endPageNumber = endPageNumber;
115
}
116
117
public int getCurrentPageNumber() {
118
return currentPageNumber;
119
}
120
121
public void setCurrentPageNumber(int currentPageNumber) {
122
this.currentPageNumber = currentPageNumber;
123
}
124
125
public int getTotalRecordCounts() {
126
return totalRecordCounts;
127
}
128
129
public void setTotalRecordCounts(int totalRecordCounts) {
130
this.totalRecordCounts = totalRecordCounts;
131
}
132
133
public int getPageSize() {
134
return pageSize;
135
}
136
137
public void setPageSize(int pageSize) {
138
this.pageSize = pageSize;
139
}
140
141
public int getTotalPageCounts() {
142
return totalPageCounts;
143
}
144
145
public void setTotalPageCounts(int totalPageCounts) {
146
this.totalPageCounts = totalPageCounts;
147
}
148
149
public int getStartIndex() {
150
return startIndex;
151
}
152
153
public void setStartIndex(int startIndex) {
154
this.startIndex = startIndex;
155
}
156
157
public List<T> getPageList() {
158
return pageList;
159
}
160
161
public void setPageList(List<T> pageList) {
162
this.pageList = pageList;
163
}
164
165
}
③Page.jsp公用分页页面
(使用方法:在要分页的页面利用<%@ include file" " %>静态包含此页面,注意此分页跳转链接中带了“分页条件,根据自己“分页条件需求”作出更改即可)
1
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3
<!-- 如果查询出的数据总条数为0,则不显示分页 -->
4
<c:if test="${not empty pb.totalPageCounts }">
5
<!-- 如果当前页为1,则不显示首页,否则显示首页 -->
6
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=1&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==1?"":"首页" }</a>
7
8
<!-- 如果当前页为1,则不显示上一页,否则显示上一页 -->
9
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${(pb.currentPageNumber-1)<=0?1:pb.currentPageNumber-1}&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==1?'':'上一页' }</a>
10
<!-- 若左端还有页码,则显示省略号,有上一页功能 -->
11
<c:if test="${pb.beginPageNumber>1 }">
12
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${pb.currentPageNumber-1}&categoryId=${sessionScope.categoryId}" style="text-decoration: none;"><strong>... </strong></a>
13
</c:if>
14
<!-- 显示数字页码,当前页数字显示红色 -->
15
<c:forEach begin="${pb.beginPageNumber }" end="${pb.endPageNumber }" var="count">
16
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${count}&categoryId=${sessionScope.categoryId}"><span style="${pb.currentPageNumber==count?'color:red':''}">${count }</span></a>
17
</c:forEach>
18
<!-- 若右端还有页码,则显示省略号 ,有下一页功能 -->
19
<c:if test="${pb.endPageNumber<pb.totalPageCounts }">
20
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${pb.currentPageNumber+1}&categoryId=${sessionScope.categoryId}" style="text-decoration: none;"><strong>... </strong></a>
21
</c:if>
22
23
<!-- 如果当前页为总页数,则不显示下一页,否则显示下一页 -->
24
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${(pb.currentPageNumber+1)>pb.totalPageCounts?pb.totalPageCounts:pb.currentPageNumber+1}&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==pb.totalPageCounts?'':'下一页' }</a>
25
<!-- 如果当前页为总页数,则不显示尾页,否则显示尾页 -->
26
<a href="${pageContext.request.contextPath }${pb.pageUri }¤tPageNumber=${pb.totalPageCounts }&categoryId=${sessionScope.categoryId}">${pb.currentPageNumber==pb.totalPageCounts?'':'尾页' }</a>
27
28
<!-- 输入框只能输入不为0的数字,为空或者为不存在的页码则还是显示当前页 -->
29
<input type="text" id="jumpPageNum" size="1" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"/> <a href="#" onclick="jumpPageNum(this);">跳转</a>
30
当前第<span style="color:red">${pb.currentPageNumber }</span>页/共<span style="color:red">${pb.totalPageCounts }</span>页 <span style="color:red">${pb.totalRecordCounts }</span>条数据
31
32
<!-- 改变每页显示的数据条数 -->
33
每页显示<select name="pageSize" onchange="changePageSize(this)">
34
<option value="5" ${sessionScope.pageSize==5?"selected='selected'":"" }>5</option>
35
<option value="10" ${sessionScope.pageSize==10?"selected='selected'":"" }>10</option>
36
<option value="15" ${sessionScope.pageSize==15?"selected='selected'":"" }>15</option>
37
</select>条数据
38
</c:if>
39
<script type="text/javascript">
40
//跳转框输入数字,跳转时调用
41
function jumpPageNum(node){
42
var jumpPageNum = document.getElementById("jumpPageNum").value;
43
if(jumpPageNum==null || jumpPageNum==""){//如果没有输入跳转页
44
jumpPageNum = "${pb.currentPageNumber}";//保持当前页
45
}
46
var totalPageCounts = "${pb.totalPageCounts}";
47
if(jumpPageNum<=0){
48
jumpPageNum = "${pb.currentPageNumber}";//保持当前页
49
}
50
/*注意js中
51
①数字和字符串比较:javascript都会把字符串转换成数字,然后按照数字顺序比较它们。
52
②字符串与字符串的比较:
53
情况1:能找到对应位置上的不同字符,那么就比较第一个不同字符的大小。
54
情况2:不能找到对应位置上的不同字符,这时候比较的是两个字符串的长度。
55
*/
56
if(parseInt(jumpPageNum)>parseInt(totalPageCounts)){//这里需要将字符串转换成数字,才能正确比较
57
jumpPageNum = "${pb.currentPageNumber}";//保持当前页
58
}
59
node.href = "${pageContext.request.contextPath }${pb.pageUri }&categoryId=${sessionScope.categoryId}¤tPageNumber="+jumpPageNum;
60
}
61
62
//改变每页显示的数据条数
63
function changePageSize(node){
64
var pageSize = node.value;
65
//注意每次改变每页显示数据条数,当前页不能作为参数代入,只要更改了每页显示条数,就必须从第一页查起,因为假如之前当前页是第5页,这样数据库查询时起始索引为(当前页-1)*每页显示条数,若结果过大,这样很可能查询不到数据。
66
window.location.href="${pageContext.request.contextPath }${pb.pageUri }&categoryId=${sessionScope.categoryId}&pageSize="+pageSize;
67
}
68
</script>
69
④分页Servlet
1
/**
2
* 根据图书种类分页查询图书
3
* @param request
4
* @param response
5
* @return
6
*/
7
public Object showBooksByCategory(HttpServletRequest request, HttpServletResponse response){
8
9
//获取请求参数分类id
10
String categoryId = request.getParameter("categoryId");
11
String categoryIdSession = (String)request.getSession().getAttribute("categoryId");
12
if(categoryId == null || categoryId == "" || categoryId.hashCode()==0){//说明是首页进入
13
categoryIdSession = null;//注意这里必须清空,当session中有categoryId时,categoryIdSession!=null, 页面点击首页时,categoryId==null,但是下面传入的是categoryIdSession的不为空值按分类查询
14
request.getSession().removeAttribute("categoryId");
15
}else{//说明是处在分类
16
categoryIdSession = categoryId;
17
request.getSession().setAttribute("categoryId", categoryIdSession);//更改session中的值
18
}
19
//获取页面传递过来的要显示的当前页码
20
String currentPageNumber = request.getParameter("currentPageNumber");
21
//获取页面传递过来的要每页显示的条数
22
String pageSize = request.getParameter("pageSize");
23
//获取session中pageSize的值
24
String pageSizeSession = (String)request.getSession().getAttribute("pageSize");
25
/**
26
* 首先判Session中是否有pageSize,没有则创建,默认值10
27
* 再判断页面传递过来的pageSize是否为null
28
* 1.如果为null 说明页面没有更改每页显示条数的需求
29
* 分页时,取session中的pageSize值,供分页使用
30
* 2.如果不为null 说明需要更改每页显示条数
31
* 则把session中的pageSize改为页面传递过来的值大小,供分页使用
32
*/
33
if(pageSizeSession == null){
34
pageSizeSession = "10";//设置session中pageSize值为10,即默认每页显示10条
35
request.getSession().setAttribute("pageSize", pageSizeSession);
36
}
37
if(pageSize != null){
38
pageSizeSession = pageSize;
39
request.getSession().setAttribute("pageSize", pageSizeSession);//更改session中的值
40
}
41
//调用service查询所有的图书
42
PageBean<Book> pb = bs.findPageBooksByCategory(currentPageNumber, categoryIdSession, pageSizeSession);
43
//设置分页页码跳转时的uri
44
pb.setPageUri("/servlet/AppServlet?method=showBooksByCategory");
45
request.setAttribute("pb", pb);
46
return request.getRequestDispatcher("/app/welcome.jsp");
47
48
}
⑤分页Service
1
@Override
2
public PageBean<Book> findPageBooksByCategory(String currentPageNumber,
3
String categoryId, String pageSize) {
4
/**
5
* 设置当前页页码
6
*/
7
int number = 1;//默认为第一页
8
//判断要显示的当前页码
9
if(currentPageNumber!=null && !"".equals(currentPageNumber)){
10
number = Integer.valueOf(currentPageNumber);
11
}
12
int totalRecordCounts = 0;
13
if(categoryId==null || categoryId=="" || categoryId.hashCode()==0){//不按分类
14
//根据获取所有书总记录数
15
totalRecordCounts = bDao.getTotalRecordCounts();
16
}else{
17
//根据图书种类,获取记录数
18
totalRecordCounts = bDao.getTotalRecordCounts(categoryId);
19
}
20
//创建PageBean对象
21
PageBean<Book> pb = new PageBean<Book>(number, totalRecordCounts, Integer.valueOf(pageSize));
22
23
//调用dao,按图书种类获取图书
24
List<Book> bs = null;
25
//TODO 分析 == null || =="" 和 !=null && !=""
26
if(categoryId==null || categoryId=="" || categoryId.hashCode()==0){//不按分类
27
bs = bDao.findPageBooks(pb.getStartIndex(), pb.getPageSize());
28
}else{
29
bs = bDao.findPageBooksByCategory(pb.getStartIndex(), pb.getPageSize(), categoryId);
30
}
31
//封装到PageBean对象中
32
pb.setPageList(bs);
33
return pb;
34
}
⑥分页Dao
1
@Override
2
public int getTotalRecordCounts(String categoryId) {
3
try{
4
//sql语句
5
String sql = "select count(*) from book where category_id = ?";
6
return qr.query(sql, new ScalarHandler<Long>(), categoryId).intValue();
7
}catch(Exception e){
8
throw new DaoRuntimeException(e);
9
}
10
}
11
12
@Override
13
public List<Book> findPageBooksByCategory(int startIndex, int pageSize,
14
String categoryId) {
15
try{
16
//sql语句
17
String sql = "select id, bName, author, price, image, bDescription, category_id from book where category_id = ? limit ?, ?";
18
return qr.query(sql, new BeanListHandler<Book>(Book.class), categoryId, startIndex, pageSize);
19
}catch(Exception e){
20
throw new DaoRuntimeException(e);
21
}
22
}