先給你們展現下分頁效果,若是親們還很滿意請參考如下代碼。java
在超連接中要保留參數web
當使用多條件查詢後,而後在點擊第2 頁時,這個第2頁超連接沒有條件了,因此會丟失條件,因此咱們須要在頁面上的全部連接都要保留條件!dom
咱們要把條件以一個字符串的形式保存到PageBean的url中!這個任務交給Servlet!jsp
pagebeanthis
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package
cn.itcast.cstm.domain;
import
java.util.List;
public
class
PageBean<T> {
private
int
pc;
// 當前頁碼page code
//private int tp;// 總頁數total page
private
int
tr;
// 總記錄數total record
private
int
ps;
// 每頁記錄數page size
private
List<T> beanList;
// 當前頁的記錄
private
String url;
//它就是url後的條件!
public
String getUrl() {
return
url;
}
public
void
setUrl(String url) {
this
.url = url;
}
public
int
getPc() {
return
pc;
}
public
void
setPc(
int
pc) {
this
.pc = pc;
}
/**
* 計算總頁數
* @return
*/
public
int
getTp() {
// 經過總記錄數和每頁記錄數來計算總頁數
int
tp = tr / ps;
return
tr%ps==
0
? tp : tp+
1
;
}
// public void setTp(int tp) {
// this.tp = tp;
// }
public
int
getTr() {
return
tr;
}
public
void
setTr(
int
tr) {
this
.tr = tr;
}
public
int
getPs() {
return
ps;
}
public
void
setPs(
int
ps) {
this
.ps = ps;
}
public
List<T> getBeanList() {
return
beanList;
}
public
void
setBeanList(List<T> beanList) {
this
.beanList = beanList;
}
}
|
jsp頁面編碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
第${pb.pc }頁/共${pb.tp }頁
<a href=
"${pb.url }&pc=1"
>首頁</a>
<c:
if
test=
"${pb.pc > 1 }"
>
<a href=
"${pb.url }&pc=${pb.pc-1}"
>上一頁</a>
</c:
if
>
<%-- 計算begin、end --%>
<c:choose>
<%-- 若是總頁數不足
10
頁,那麼把全部的頁數都顯示出來! --%>
<c:when test=
"${pb.tp <= 10 }"
>
<c:set var=
"begin"
value=
"1"
/>
<c:set var=
"end"
value=
"${pb.tp }"
/>
</c:when>
<c:otherwise>
<%-- 當總頁數>
10
時,經過公式計算出begin和end --%>
<c:set var=
"begin"
value=
"${pb.pc-5 }"
/>
<c:set var=
"end"
value=
"${pb.pc+4 }"
/>
<%-- 頭溢出 --%>
<c:
if
test=
"${begin < 1 }"
>
<c:set var=
"begin"
value=
"1"
/>
<c:set var=
"end"
value=
"10"
/>
</c:
if
>
<%-- 尾溢出 --%>
<c:
if
test=
"${end > pb.tp }"
>
<c:set var=
"begin"
value=
"${pb.tp - 9 }"
/>
<c:set var=
"end"
value=
"${pb.tp }"
/>
</c:
if
>
</c:otherwise>
</c:choose>
<%-- 循環遍歷頁碼列表 --%>
<c:forEach var=
"i"
begin=
"${begin }"
end=
"${end }"
>
<c:choose>
<c:when test=
"${i eq pb.pc }"
>
[${i }]
</c:when>
<c:otherwise>
<a href=
"${pb.url }&pc=${i}"
>[${i }]</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:
if
test=
"${pb.pc < pb.tp }"
>
<a href=
"${pb.url }&pc=${pb.pc+1}"
>下一頁</a>
</c:
if
>
<a href=
"${pb.url }&pc=${pb.tp}"
>尾頁</a>
|
servleturl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package
cn.itcast.cstm.web.servlet;
import
java.io.IOException;
import
java.io.UnsupportedEncodingException;
import
java.util.List;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
cn.itcast.commons.CommonUtils;
import
cn.itcast.cstm.domain.Customer;
import
cn.itcast.cstm.domain.PageBean;
import
cn.itcast.cstm.service.CustomerService;
import
cn.itcast.servlet.BaseServlet;
public
class
CustomerServlet
extends
BaseServlet {
private
CustomerService customerService =
new
CustomerService();
/**
* 獲取pc
*
* @param request
* @return
*/
private
int
getPc(HttpServletRequest request) {
/*
* 1. 獲得pc 若是pc參數不存在,說明pc=1 若是pc參數存在,須要轉換成int類型便可
*/
String value = request.getParameter("pc");
if (value == null || value.trim().isEmpty()) {
return 1;
}
return Integer.parseInt(value);
}
//分頁servlet
public String query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println(getUrl(request));
/*
* 0. 把條件封裝到Customer對象中 1. 獲得pc 2. 給定ps 3.
* 使用pc和ps,以及條件對象,調用service方法獲得PageBean 4. 把PageBean保存到request域中 5.
* 轉發到list.jsp
*/
// 獲取查詢條件
Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class);
/*
* 處理GET請求方式編碼問題!
*/
criteria = encoding(criteria);
int pc = getPc(request);// 獲得pc
int ps = 10;// 給定ps的值,第頁10行記錄
PageBean<Customer> pb = customerService.query(criteria, pc, ps);
// 獲得url,保存到pb中
pb.setUrl(getUrl(request));
request.setAttribute("pb", pb);
return "f:/list.jsp";
}
/**
* 處理四樣
*
* @param criteria
* @return
* @throws UnsupportedEncodingException
*/
private Customer encoding(Customer criteria) throws UnsupportedEncodingException {
String cname = criteria.getCname();
String gender = criteria.getGender();
String cellphone = criteria.getCellphone();
String email = criteria.getEmail();
if (cname != null && !cname.trim().isEmpty()) {
cname = new String(cname.getBytes("ISO-8859-1"), "utf-8");
criteria.setCname(cname);
}
if (gender != null && !gender.trim().isEmpty()) {
gender = new String(gender.getBytes("ISO-8859-1"), "utf-8");
criteria.setGender(gender);
}
if (cellphone != null && !cellphone.trim().isEmpty()) {
cellphone = new String(cellphone.getBytes("ISO-8859-1"), "utf-8");
criteria.setCellphone(cellphone);
}
if (email != null && !email.trim().isEmpty()) {
email = new String(email.getBytes("ISO-8859-1"), "utf-8");
criteria.setEmail(email);
}
return criteria;
}
/**
* 截取url /項目名/Servlet路徑?參數字符串
*
* @param request
* @return
*/
private
String getUrl(HttpServletRequest request) {
String contextPath = request.getContextPath();
// 獲取項目名
String servletPath = request.getServletPath();
// 獲取servletPath,即/CustomerServlet
String queryString = request.getQueryString();
// 獲取問號以後的參數部份
// 判斷參數部份中是否包含pc這個參數,若是包含,須要截取下去,不要這一部份。
if
(queryString.contains(
"&pc="
)) {
int
index = queryString.lastIndexOf(
"&pc="
);
queryString = queryString.substring(
0
, index);
}
return
contextPath + servletPath +
"?"
+ queryString;
}
}
|
以上內容是是小編給你們分享的JSP通用高大上分頁代碼(超管用),但願對你們有所幫助。spa