首页 » 编程笔记 » 未分类 » 正文

java 简单实用分页代码笔记

分页样式用的是bootstrap,实现如下图:

WangWang20161207190503

java 部分代码

int total =  0;
int perpage = 30;
int pagenum = 0;
if ((total % perpage) == 0) {
    pagenum = total / perpage;
} else {
    pagenum = total / perpage + 1;
}
int befpage = page-1;
int nextpage = page+1;
if(page<=1) {
    page = 1;
    befpage =  page;
}

if(page >= pagenum){
    page = pagenum;
    nextpage = page;
}
int start = (page-1)* perpage;
int page_window_size = 5;
int page_window_start = page - 2;
int page_window_end = page + 2;
if(page_window_start <= 0){
    page_window_start = 1;
    page_window_end = page_window_size;
}
if(page_window_end > pagenum){
    page_window_end = pagenum;
}

context.put("total", total);
context.put("page", page);
context.put("perpage", perpage);
context.put("befpage", befpage);
context.put("nextpage", nextpage);
context.put("page_window_start", page_window_start);
context.put("page_window_end", page_window_end);

模板部分代码

#if($total > $perpage)
    <nav>
      <ul class=”pagination”>
        <li><a href=”xx”>«</a></li>
        #foreach($p in [$page_window_start..$page_window_end])
        #if($p == $page)
        <li class=”active”><a href=”xx”>$!{p} <span class=”sr-only”>(current)</span></a></li>
        #else
        <li><a href=”xx”>$!{p}</a></li>
        #end
        #end
        <li><a href=”xxx$!{nextpage}>»</a></li>
     </ul>
    </nav>
    #end