springmvc文章分页源代码

点击打开链接

由于网友的需求,我把本站【领悟书生教程网】的文章分页代码分享给各位,代码比较简单,在使用的过程中如果有什么不明白可以找我,同时希望各位提出诚恳的意见


分页效果



首先来看看分页工具类PageUtil.java:

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package  com.naxsu.utils;
  
/**
  * 分页工具包
  * @author huangyineng
  * 2012-10-28 下午11:24:25 http://www.656463.com
  */
public  class  PageUtil {
     private  int  pageRecord; // 每页面的记录数
     private  int  totalRecord; // 总记录数
     private  int  totalPage; // 总页数
     private  int  currentPage =  1 ; // 当前页
  
     private  int  prePage; // 上一页
     private  int  nextPage; // 下一页
  
     private  int  pageNumStart; // 页码显示开始listbegin;
     private  int  pageNumEnd; // 页码显示结束listend;
     private  int  showPageNum =  10 ; // 显示页码个数,默认是10
      
     public  PageUtil() {
          
     }
      
     public  PageUtil( int  pageRecord,  int  totalRecord,  int  currentPage) {
         this .pageRecord = pageRecord;
//      this.totalRecord = totalRecord;
         this .currentPage = currentPage;
         setTotalRecord(totalRecord);
         setPageNumEnd(pageNumEnd);
         setPageNumStart(pageNumStart);
     }
  
     public  int  getPageRecord() {
         return  pageRecord;
     }
  
     public  void  setPageRecord( int  pageRecord) {
         this .pageRecord = pageRecord;
     }
  
     public  int  getTotalRecord() {
         return  totalRecord;
     }
  
     public  void  setTotalRecord( int  totalRecord) {
         this .totalRecord = totalRecord;
         setTotalPage( this .totalRecord %  this .pageRecord ==  0  ? this .totalRecord
                 this .pageRecord :  this .totalRecord /  this .pageRecord + 1 );
     }
  
     public  int  getTotalPage() {
         return  totalPage;
     }
  
     public  void  setTotalPage( int  totalPage) {
         this .totalPage = totalPage;
     }
  
     // 获取当前页
     public  int  getCurrentPage() {
         return  currentPage;
     }
  
     // 设置当前页面
     public  void  setCurrentPage( int  currentPage) {
         // 如果当前页数大于总页数,即当前页等于总页面数
         if  (currentPage > getTotalPage()) {
             this .currentPage = getTotalPage();
         else  {
             if  (currentPage <  1 ) {
                 this .currentPage =  1 ; // 如果当前页小于1,默认是1
             else  {
                 this .currentPage = currentPage;
             }
         }
     }
  
     // 获取下一页
     public  int  getNextPage() {
         return  this .getCurrentPage() +  1 ;
     }
  
     // 获取上一页
     public  int  getPrePage() {
         return  this .getCurrentPage() -  1 ;
     }
  
     public  int  getPageNumStart() {
         return  pageNumStart;
     }
  
     public  void  setPageNumStart( int  pageNumStart) {
         // 显示页数的一半
         int  halfPage = ( int ) Math.ceil(( double ) showPageNum /  2 );
         if  (halfPage >= currentPage) {
             this .pageNumStart =  1 ;
         else  {
             if  (currentPage + halfPage > totalPage) {
                 this .pageNumStart = (totalPage - showPageNum +  1 )<= 0 ? 1 :
                                     (totalPage - showPageNum +  1 );
             else  {
                 this .pageNumStart = currentPage - halfPage +  1 ;
             }
         }
     }
  
     public  int  getPageNumEnd() {
         return  pageNumEnd;
     }
  
     public  void  setPageNumEnd( int  pageNumEnd) {
         // 显示页数的一半
         int  halfPage = ( int ) Math.ceil(( double ) showPageNum /  2 );
         if  (halfPage >= currentPage) {
             //this.pageNumEnd = showPageNum;
             this .pageNumEnd = showPageNum>totalPage?totalPage:showPageNum;
         else  {
             if  (currentPage + halfPage >= totalPage) {
                 this .pageNumEnd = totalPage;
             else  {
                 this .pageNumEnd = currentPage + halfPage;
             }
         }
     }
  
     public  int  getShowPageNum() {
         return  showPageNum;
     }
  
     public  void  setShowPageNum( int  showPageNum) {
         this .showPageNum = showPageNum;
     }
  
     // 传给mybatis,limit start,size
     private  int  start;
     private  int  size;
  
     public  int  getStart() {
         return  ( this .currentPage -  1 ) * pageRecord ; //+ 1;
     }
  
     public  int  getSize() {
         return  this .currentPage * pageRecord;
     }
  
}

控制层:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@RequestMapping (value =  "/tutorials" , method = RequestMethod.GET)
public  String getTutorialsByChannelType(String page, Model model) {
  
     int  totalArticles =  this .getArticleservice().getArticleCount(); // 总数
  
     int  currentPage= 1 ;
     if  (page !=  null ) {
         currentPage=Integer.parseInt(page);
     }
      
     PageUtil _page =  new  PageUtil( 10 ,totalArticles,currentPage);
     _page.setTotalRecord(totalArticles);
     _page.setPageNumStart(_page.getPageNumStart());
     _page.setPageNumEnd(_page.getPageNumEnd());
     _page.setCurrentPage(currentPage);
      
     Map map =  new  HashMap();
     map.put( "start" ,_page.getStart());
     map.put( "size" , _page.getPageRecord());
     model.addAttribute( "articles" , this .getArticleservice().getPageArticles(map));
     model.addAttribute( "showPage" , _page);
  
     return  "article/getTutorials" ;
}

页面显示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<div  class = "pg" >
     <c: if  test= "${showPage.totalPage>0}" >
         <c: if  test= "${showPage.currentPage!=1}" >
             <a href= "/tutorials?page=${showPage.prePage }" class = "prev" >&nbsp;&nbsp;</a>
         </c: if >
         <c:forEach begin= "${showPage.pageNumStart }" end= "${showPage.pageNumEnd }"  var= "pageIndex" >
             <c: if  test= "${showPage.currentPage==pageIndex }" >
                 <strong>${pageIndex }</strong>
             </c: if >
             <c: if  test= "${showPage.currentPage!=pageIndex }" >
                 <a href= "/tutorials?page=${pageIndex }" >${pageIndex }</a>
             </c: if >
         </c:forEach>
         <c: if  test= "${showPage.currentPage!=showPage.totalPage}" >
             <a href= "/tutorials?page=${showPage.nextPage }"  class = "nxt" >下一页</a>
         </c: if >
     </c: if >
</div>

相关代码贴完了,希望对你有用


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值