Blog系统开发 15. 小细节 (3) 分页问题 PageTag类
对于表格数据处理的话,displayTag有很大的优势;但是有些时候还得自己动手做点工作;
我自己写了一个PageTag的类来处理一些简单的工作。
PageTag.java
随后在查询blog的时候,我们再次使用 pageTag.getStartPos()和 pageTag.getPageSize()这两个方法,取得查询数据开始的位置与长度。Job Done!
再看jsp页面
line 8 取得一个基数base(类似10,20,30);
line 9 取得余数;
line 10 如果i加上基数base 大于 总页数的话,就没有必要再继续打印页码了;
line 15 如果i不等于余数的话,
line 16 为该页码添加链接;
line 17 否则,不必为该页码添加链接。
因为数据不多,之前的pageSize我改成了1;让我们看下效果:
我自己写了一个PageTag的类来处理一些简单的工作。
PageTag.java
1
package
com.blog.utils;
2
3 /**
4 *
5 * @author Chucky
6 */
7 public class PageTag {
8 /* pageNo: the page number of current page
9 * pageSize: how many records in one page
10 * totalPages: the number of totalpages
11 */
12
13 private int pageNo;
14 private int pageSize;
15 private int totalPages;
16
17 public PageTag( int pageNo, int pageSize) {
18 this .pageNo = pageNo;
19 this .pageSize = pageSize;
20 this .totalPages = 1 ;
21 }
22
23 // accroding to the number of records to calculate the totalpages
24 public PageTag( int pageNo, int pageSize, int records) {
25 this .pageNo = pageNo;
26 this .pageSize = pageSize;
27 this .totalPages = countPages(records);
28 }
29
30 public int getPageNo() {
31 return pageNo;
32 }
33
34 public void setPageNo( int pageNo) {
35 this .pageNo = pageNo;
36 }
37
38 public int getPageSize() {
39 return pageSize;
40 }
41
42 public void setPageSize( int pageSize) {
43 this .pageSize = pageSize;
44 }
45
46 public int getTotalPages() {
47 return totalPages;
48 }
49
50 public void setTotalPages( int totalPages) {
51 this .totalPages = totalPages;
52 }
53
54 public int countPages( int records) {
55 totalPages = records % pageSize == 0 ? records / pageSize : records / pageSize + 1 ;
56 return totalPages;
57 }
58
59 /* getStartPos: get the start postion of the records
60 */
61 public int getStartPos() {
62 int startPos = (pageNo - 1 ) * pageSize;
63 return startPos;
64 }
65
66 /* get the page number of previos page
67 */
68 public int getPrePage() {
69 int prePage = pageNo - 1 ;
70 if (prePage == 0 ) {
71 prePage = pageNo;
72 }
73 return prePage;
74 }
75
76 /* get the page number of next page
77 */
78 public int getNextPage() {
79 int nextPage = pageNo + 1 ;
80 if (nextPage > totalPages) {
81 nextPage = totalPages;
82 }
83 return nextPage;
84 }
85
86 /* get the page number of previos page which specified
87 */
88 public int getPrePage( int page) {
89 int prePage = page - 1 ;
90 if (prePage == 0 ) {
91 prePage = page;
92 }
93 return prePage;
94 }
95
96 /* get the page number of next page which specified
97 */
98 public int getNextPage( int page) {
99 int nextPage = page + 1 ;
100 if (nextPage > totalPages) {
101 nextPage = totalPages;
102 }
103 return nextPage;
104 }
105 }
这个类写的很简单,注释也很清楚;还是让我们结合项目来看看如何使用。
2
3 /**
4 *
5 * @author Chucky
6 */
7 public class PageTag {
8 /* pageNo: the page number of current page
9 * pageSize: how many records in one page
10 * totalPages: the number of totalpages
11 */
12
13 private int pageNo;
14 private int pageSize;
15 private int totalPages;
16
17 public PageTag( int pageNo, int pageSize) {
18 this .pageNo = pageNo;
19 this .pageSize = pageSize;
20 this .totalPages = 1 ;
21 }
22
23 // accroding to the number of records to calculate the totalpages
24 public PageTag( int pageNo, int pageSize, int records) {
25 this .pageNo = pageNo;
26 this .pageSize = pageSize;
27 this .totalPages = countPages(records);
28 }
29
30 public int getPageNo() {
31 return pageNo;
32 }
33
34 public void setPageNo( int pageNo) {
35 this .pageNo = pageNo;
36 }
37
38 public int getPageSize() {
39 return pageSize;
40 }
41
42 public void setPageSize( int pageSize) {
43 this .pageSize = pageSize;
44 }
45
46 public int getTotalPages() {
47 return totalPages;
48 }
49
50 public void setTotalPages( int totalPages) {
51 this .totalPages = totalPages;
52 }
53
54 public int countPages( int records) {
55 totalPages = records % pageSize == 0 ? records / pageSize : records / pageSize + 1 ;
56 return totalPages;
57 }
58
59 /* getStartPos: get the start postion of the records
60 */
61 public int getStartPos() {
62 int startPos = (pageNo - 1 ) * pageSize;
63 return startPos;
64 }
65
66 /* get the page number of previos page
67 */
68 public int getPrePage() {
69 int prePage = pageNo - 1 ;
70 if (prePage == 0 ) {
71 prePage = pageNo;
72 }
73 return prePage;
74 }
75
76 /* get the page number of next page
77 */
78 public int getNextPage() {
79 int nextPage = pageNo + 1 ;
80 if (nextPage > totalPages) {
81 nextPage = totalPages;
82 }
83 return nextPage;
84 }
85
86 /* get the page number of previos page which specified
87 */
88 public int getPrePage( int page) {
89 int prePage = page - 1 ;
90 if (prePage == 0 ) {
91 prePage = page;
92 }
93 return prePage;
94 }
95
96 /* get the page number of next page which specified
97 */
98 public int getNextPage( int page) {
99 int nextPage = page + 1 ;
100 if (nextPage > totalPages) {
101 nextPage = totalPages;
102 }
103 return nextPage;
104 }
105 }
1
List blogs
=
null
;
2
3
4 /* pages proceesor:
5 * get related the number of records (such as blogs number);
6 * then calculate totalpages;
7 * store pageTag to request if nesscessary.
8 */
9 String page = request.getParameter( " page " );
10 int pageSize = 10 ;
11 int pageNo = 1 ;
12 if (page != null ) {
13 pageNo = new Integer(page);
14 }
15 PageTag pageTag = new PageTag(pageNo, pageSize);
16
17 if (cid == null ) {
18 sql = " select b.id as id from blog b, category c where category_id = c.id " ;
19 } else {
20 sql = " select b.id as id from blog b, category c where category_id = " + cid + " and category_id = c.id " ;
21 }
22 try {
23 blogs = (List) qr.query(sql, new BeanListHandler(Blog. class ));
24 if (blogs.size() > 0 ) {
25 pageTag.countPages(blogs.size());
26 }
27 } catch (SQLException ex) {
28 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
29 }
30 request.setAttribute( " pageTag " , pageTag);
31
32
33 /* blog collection
34 * if cid not equals null which means it queries all blogs that belong to specified category
35 * otherwise get all blogs
36 */
37
38 if (cid == null ) {
39 // sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = c.id order by date desc";
40 sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
41 " (select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
group by blog.id) as b, category c " +
42 " where categoryId = c.id " +
43 " order by date desc " +
44 " limit " + pageTag.getStartPos() + " , " + pageTag.getPageSize();
45 } else {
46 sql = " select id,name from category where id = " + cid;
47 try {
48 List list = (List) qr.query(sql, new BeanListHandler(Category. class ));
49 Category category = (Category) list.get( 0 );
50 request.setAttribute( " category " , category);
51 } catch (SQLException ex) {
52 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
53 }
54 // sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = " + cid + " and category_id = c.id order by date desc";
55 sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
56 " (select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
group by blog.id) as b, category c " +
57 " where categoryId = " + cid + " and categoryId = c.id " +
58 " order by date desc " +
59 " limit " + pageTag.getStartPos() + " , " + pageTag.getPageSize();
60 }
61 try {
62 blogs = (List) qr.query(sql, new BeanListHandler(Blog. class ));
63 } catch (SQLException ex) {
64 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
65 }
66 request.setAttribute( " blogs " , blogs);
我们可以看到4-30行是有关pageTag的使用;首先从request取得当前页(page属性),接着创建pageTag对象,根据需求(cid)取得blog的数量;从而得到总页数;最后将pageTag对象保存至request中。
2
3
4 /* pages proceesor:
5 * get related the number of records (such as blogs number);
6 * then calculate totalpages;
7 * store pageTag to request if nesscessary.
8 */
9 String page = request.getParameter( " page " );
10 int pageSize = 10 ;
11 int pageNo = 1 ;
12 if (page != null ) {
13 pageNo = new Integer(page);
14 }
15 PageTag pageTag = new PageTag(pageNo, pageSize);
16
17 if (cid == null ) {
18 sql = " select b.id as id from blog b, category c where category_id = c.id " ;
19 } else {
20 sql = " select b.id as id from blog b, category c where category_id = " + cid + " and category_id = c.id " ;
21 }
22 try {
23 blogs = (List) qr.query(sql, new BeanListHandler(Blog. class ));
24 if (blogs.size() > 0 ) {
25 pageTag.countPages(blogs.size());
26 }
27 } catch (SQLException ex) {
28 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
29 }
30 request.setAttribute( " pageTag " , pageTag);
31
32
33 /* blog collection
34 * if cid not equals null which means it queries all blogs that belong to specified category
35 * otherwise get all blogs
36 */
37
38 if (cid == null ) {
39 // sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = c.id order by date desc";
40 sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
41 " (select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
group by blog.id) as b, category c " +
42 " where categoryId = c.id " +
43 " order by date desc " +
44 " limit " + pageTag.getStartPos() + " , " + pageTag.getPageSize();
45 } else {
46 sql = " select id,name from category where id = " + cid;
47 try {
48 List list = (List) qr.query(sql, new BeanListHandler(Category. class ));
49 Category category = (Category) list.get( 0 );
50 request.setAttribute( " category " , category);
51 } catch (SQLException ex) {
52 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
53 }
54 // sql = "select b.id as id, title, content, date, c.name as category, c.id as categoryId from blog b, category c where category_id = " + cid + " and category_id = c.id order by date desc";
55 sql = " select b.id as id,b.title as title,b.content as content,b.date as date,c.name as category,categoryId,comments from " +
56 " (select blog.id as id ,blog.title as title,blog.category_id as categoryId,count(comment.blog_id) as comments,blog.content as content,blog.date as date from blog left join comment on blog.id = comment.blog_id
group by blog.id) as b, category c " +
57 " where categoryId = " + cid + " and categoryId = c.id " +
58 " order by date desc " +
59 " limit " + pageTag.getStartPos() + " , " + pageTag.getPageSize();
60 }
61 try {
62 blogs = (List) qr.query(sql, new BeanListHandler(Blog. class ));
63 } catch (SQLException ex) {
64 Logger.getLogger(HomeServlet. class .getName()).log(Level.SEVERE, null , ex);
65 }
66 request.setAttribute( " blogs " , blogs);
随后在查询blog的时候,我们再次使用 pageTag.getStartPos()和 pageTag.getPageSize()这两个方法,取得查询数据开始的位置与长度。Job Done!
再看jsp页面
1
<
div
id
="pagetags"
>
2 <% if (pageTag.getTotalPages() ! = 0 ){
3 int totalPages = pageTag.getTotalPages();
4 if (totalPages > 1 ) { %>
5 < span class ="pagelinks" > [ < a href ="<%=request.getContextPath()%>/HomeServlet?page=1" > 首页 </ a > / < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getPrePage()%>" > 上一页 </ a > ]
6
7 <% for ( int i = 1 ;i <= 10 ;i ++ ){
8 int base = (pageTag.getPageNo() / 10 )*10;
9 int remainder = pageTag.getPageNo()% 10 ;
10 if ((i + base ) > pageTag.getTotalPages()){
11 break;
12 }
13 if (i ! = remainder){
14 %>
15 < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=i+ base %>" > <% = i + base %> </ a >
16 <% } else { %>
17 < strong > <% = pageTag.getPageNo() %> </ strong >
18 <% }} %>
19
20 [ < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getNextPage()%>" > 下一页 </ a > / < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=totalPages%>" > 尾页 </ a > ] </ span >
21 <% } else { %>
22 < span class ="pagelinks" > 共1页 </ span >
23 <% }} %>
24 </ div >
首页很简单,page设为1即可;尾页的话就totalPage的值;这里集中要说一下for循环的内容。
2 <% if (pageTag.getTotalPages() ! = 0 ){
3 int totalPages = pageTag.getTotalPages();
4 if (totalPages > 1 ) { %>
5 < span class ="pagelinks" > [ < a href ="<%=request.getContextPath()%>/HomeServlet?page=1" > 首页 </ a > / < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getPrePage()%>" > 上一页 </ a > ]
6
7 <% for ( int i = 1 ;i <= 10 ;i ++ ){
8 int base = (pageTag.getPageNo() / 10 )*10;
9 int remainder = pageTag.getPageNo()% 10 ;
10 if ((i + base ) > pageTag.getTotalPages()){
11 break;
12 }
13 if (i ! = remainder){
14 %>
15 < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=i+ base %>" > <% = i + base %> </ a >
16 <% } else { %>
17 < strong > <% = pageTag.getPageNo() %> </ strong >
18 <% }} %>
19
20 [ < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=pageTag.getNextPage()%>" > 下一页 </ a > / < a href ="<%=request.getContextPath()%>/HomeServlet?page=<%=totalPages%>" > 尾页 </ a > ] </ span >
21 <% } else { %>
22 < span class ="pagelinks" > 共1页 </ span >
23 <% }} %>
24 </ div >
line 8 取得一个基数base(类似10,20,30);
line 9 取得余数;
line 10 如果i加上基数base 大于 总页数的话,就没有必要再继续打印页码了;
line 15 如果i不等于余数的话,
line 16 为该页码添加链接;
line 17 否则,不必为该页码添加链接。
因为数据不多,之前的pageSize我改成了1;让我们看下效果: