對(duì)于web應(yīng)用來(lái)說(shuō)分頁(yè)顯示數(shù)據(jù)是是最基本的功能,作為經(jīng)常開發(fā)web應(yīng)用的程序員來(lái)說(shuō)這個(gè)功能也小菜一碟,但是對(duì)于初學(xué)者來(lái)說(shuō)卻是是個(gè)常見的問題,以前自學(xué)web開發(fā)的時(shí)候?qū)@個(gè)問題也是各種google,整了半天才實(shí)現(xiàn)。
現(xiàn)在手中的項(xiàng)目正好也需要一個(gè)分頁(yè)的功能,這個(gè)項(xiàng)目用了spring mvc數(shù)據(jù)庫(kù)用到了mongodb,自己就寫了一個(gè)分頁(yè)的標(biāo)簽供整個(gè)項(xiàng)目來(lái)使用,今天周六加完班閑著沒事就把分頁(yè)的實(shí)現(xiàn)寫出來(lái)以便大家參考,不當(dāng)之處歡迎批評(píng)指正。
1)分頁(yè):
一般都是把一次查詢分幾次查詢來(lái)顯示。用戶每次點(diǎn)擊頁(yè)數(shù)(或者上一頁(yè)下一頁(yè))的時(shí)候?qū)嶋H上一次查詢請(qǐng)求。查詢?cè)O(shè)定數(shù)據(jù)的條數(shù)返回并顯示。
2)mongodb分頁(yè)用到的工具
在查詢的時(shí)候需要用到Query來(lái)保存用戶的查詢條件,該類有兩個(gè)方法是實(shí)現(xiàn)分頁(yè)功能的核心一個(gè)是skip(int),一個(gè)是limit(int)方法,其中l(wèi)imit用來(lái)限制每次查詢的條數(shù),也是每顯示的條數(shù)。而skip是跳過當(dāng)前頁(yè)之前的所有頁(yè)面的數(shù)據(jù)條數(shù)開始查詢
3)分頁(yè)關(guān)鍵點(diǎn)(所需的數(shù)據(jù)):
i) 每頁(yè)顯示的條數(shù)pageSize
ii)當(dāng)前的頁(yè)數(shù)currentPage,而前面的skip方法傳入的參數(shù)就是根據(jù)currentPage和pageSize來(lái)決定的,skip = (currentPage-1)*pageSize
4)分頁(yè)的具體實(shí)現(xiàn)
通過上面的分析,分頁(yè)簡(jiǎn)單來(lái)說(shuō)就是用戶點(diǎn)擊分頁(yè)的時(shí)候提交一個(gè)關(guān)鍵數(shù)據(jù)(currentPage)到后臺(tái),后臺(tái)根據(jù)currentPage來(lái)進(jìn)行分頁(yè)查詢;至于上面的pageSize,
在后臺(tái)設(shè)置一個(gè)變量來(lái)控制即可。
下面是項(xiàng)目中的用來(lái)查詢的基類:MongodbBaseDao<T>的部分代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//每頁(yè)顯示五條 protected static final int PAGE_SIZE = 8 ; /** * 通過條件查詢,查詢分頁(yè)結(jié)果 */ public Pagination<T> getPage( int currentPage, Query query) { //獲取總條數(shù) long totalCount = this .mongoTemplate.count(query, this .getEntityClass()); //總頁(yè)數(shù) int totalPage = ( int ) (totalCount/PAGE_SIZE); int skip = (currentPage- 1 )*PAGE_SIZE; Pagination<T> page = new Pagination<T>(currentPage, totalPage, ( int )totalCount); query.skip(skip); // skip相當(dāng)于從那條記錄開始 query.limit(PAGE_SIZE); // 從skip開始,取多少條記錄 List<T> datas = this .find(query); page.build(datas); //獲取數(shù)據(jù) return page; } |
上面的代碼中Pagination類保存了分頁(yè)所需的必要的數(shù)據(jù),比如當(dāng)前頁(yè)currentPage,總頁(yè)數(shù)totalPage,總條數(shù)totalCount,當(dāng)然還有數(shù)據(jù)集合List 用來(lái)保存頁(yè)面所需的數(shù)據(jù)。另外getEntityClass()是T所對(duì)應(yīng)的class對(duì)象(由子類來(lái)具體實(shí)現(xiàn))。例如項(xiàng)目中有一個(gè)子類VideoMongodbDao
1
2
3
4
5
6
7
8
|
@Service public class VideoMongodbDao extends MongodbBaseDao<Video> { @Override protected Class<Video> getEntityClass() { return Video. class ; } } |
分頁(yè)數(shù)據(jù)封裝類Pagination<T>,里面提供了分頁(yè)的數(shù)據(jù)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public class Pagination<T> { /** 每頁(yè)顯示條數(shù) */ private Integer pageSize = 8 ; /** 當(dāng)前頁(yè) */ private Integer currentPage = 1 ; /** 總頁(yè)數(shù) */ private Integer totalPage = 1 ; /** 查詢到的總數(shù)據(jù)量 */ private Integer totalNumber = 0 ; /** 數(shù)據(jù)集 */ private List items; public Integer getPageSize() { return pageSize; } |
Pagination類還有一個(gè)重要的build方法,根據(jù)該類封裝的數(shù)據(jù)來(lái)設(shè)定分了多少頁(yè),具體實(shí)現(xiàn)方法如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * 處理查詢后的結(jié)果數(shù)據(jù) * * @param items * 查詢結(jié)果集 * @param count * 總數(shù) */ public void build(List items) { this .setItems(items); int count = this .getTotalNumber(); int divisor = count / this .getPageSize(); int remainder = count % this .getPageSize(); this .setTotalPage(remainder == 0 ? divisor == 0 ? 1 : divisor : divisor + 1 ); } |
所以在對(duì)應(yīng)的controller對(duì)應(yīng)的方法中查詢的時(shí)候,可以這么處理
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Query query = new Query(); Criteria channleIdCri = Criteria.where( "channelId" ).is(channel_id); query.addCriteria(channleIdCri); //獲取當(dāng)前頁(yè) String currentPageStr = request.getParameter( "currentPage" ); int currentPage = 0 ; if (currentPageStr != null ){ currentPage = Integer.valueOf(currentPageStr); } Pagination<Video> videos = dao.getPage(currentPage, query); m.addAttribute( "videos" , videos); //from |
下面需要生成分頁(yè)當(dāng)行條,在這里用到了spring的標(biāo)簽來(lái)處理,對(duì)應(yīng)的是標(biāo)簽類PaginationTag,該類封裝了頁(yè)面表單form對(duì)應(yīng)的id,分頁(yè)顯示所需的數(shù)據(jù),以及分頁(yè)顯示可點(diǎn)擊的頁(yè)面的長(zhǎng)度。該類如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class PaginationTag extends TagSupport { /** 列表頁(yè)面的form標(biāo)簽id值 */ private String form; /** 與后臺(tái)交互,保存在request中的 數(shù)據(jù),該數(shù)據(jù)包含l */ private String pageInfo; //request對(duì)應(yīng)的bean包含了分頁(yè)的一些數(shù)據(jù) /** 分頁(yè)顯示可點(diǎn)擊頁(yè)數(shù)的長(zhǎng)度 */ private int size; public PaginationTag(){ this .form = "form" ; this .size = 5 ; } |
生成的分頁(yè)導(dǎo)航條的代碼如下
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
|
@Override public int doStartTag() throws JspException { int half = size / 2 ; int point = size / 2 + size % 2 ; int start = 1 ; int loop = size; Pagination query = (Pagination) this .pageContext.getRequest().getAttribute(pageInfo); int pageSize = query.getPageSize(); int currentPage = query.getCurrentPage(); int totalPage = query.getTotalPage(); long totalNumber = query.getTotalNumber(); if (totalPage <= size){ start = 1 ; loop = totalPage; } else { if (currentPage > point && currentPage < totalPage - half + 1 ){ start = 1 + (currentPage - point); } else if (currentPage > totalPage - half){ start = totalPage - size + 1 ; } } if (currentPage<= 0 ){ currentPage = 1 ; } StringBuilder sb = new StringBuilder(); sb.append( "<div class=\"box-ttl\"><div class=\"g4\">共 <span class=\"text-info\">" ); sb.append(totalNumber).append( "</span> 條數(shù)據(jù)" ); sb.append( "/共 <span class=\"text-info\">" ); if (totalNumber!= 0 ){ sb.append(totalPage).append( "</span> 頁(yè)!</div><div class=\"box collapsed g6 flt-r\"><ul class=\"nav-menu\">" ); } else { sb.append( 0 ).append( "</span> 頁(yè)!</div><div class=\"box collapsed g6 flt-r\"><ul class=\"nav-menu\">" ); } // 處理上一頁(yè) if (currentPage == 1 ){ sb.append( "<li class=\"disabled\"><a href=\"#\">上一頁(yè)</a></li>" ); } else { sb.append( "<li><a href=\"javascript:dopage(" + (currentPage - 1 ) + ",'" + form + "');\">上一頁(yè)</a></li>" ); } // 處理中間數(shù)據(jù) for ( int i = start; i < start + loop; i++){ //<li class="active"><a href="#" rel="external nofollow" rel="external nofollow" >1</a></li> if (currentPage == i){ sb.append( "<li class=\"active\"><a href=\"#\">" + i + "</a></li>" ); } else { //<li><a href="#" rel="external nofollow" rel="external nofollow" >2</a></li> sb.append( "<li><a href=\"javascript:dopage(" + i + ",'" + form + "');\">" + i + "</a></li>" ); } } // 處理下一頁(yè) if (currentPage == totalPage){ sb.append( "<li class=\"disabled\"><a href=\"#\">下一頁(yè)</a></li>" ); } else { sb.append( "<li><a href=\"javascript:dopage(" + (currentPage + 1 ) + ",'" + form + "');\">下一頁(yè)</a></li>" ); } sb.append( "</ul></div></div>" ); sb.append( "<input type=\"hidden\" id=\"currentPage\" name=\"currentPage\" value=\"" + currentPage + "\"/>" ); try { pageContext.getOut().write(sb.toString()); } catch (IOException e) { throw new JspException(e.toString(), e); } return super .doStartTag(); } |
有上面的標(biāo)簽可以發(fā)現(xiàn),每次點(diǎn)擊頁(yè)面頁(yè)數(shù)的時(shí)候是用過js的dopage方法來(lái)實(shí)現(xiàn)的,該js方法根據(jù)form.submit()來(lái)提交信息查詢信息(特別是currentPage數(shù)據(jù))
js的代碼如下
1
2
3
4
|
function dopage(currentPage,formid){ $( "#currentPage" ).val(currentPage); $( "#" + formid).submit(); } |
在jsp頁(yè)面中只需要添加上述標(biāo)簽即可(標(biāo)簽類Pagination以及轉(zhuǎn)換成tld文件了—)
1
2
3
|
< div > < tv:pagination pageInfo = "videos" form = "video-form" size = "5" ></ tv:pagination > </ div > |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/chunqiuwei/article/details/17188523