分页类
这个适用情况:
适用于前端页面已提供分页按钮样式的情况
分页规则:
首页,尾页,上页,下页 这四个按钮必定出现,中间分页动态生成5个
如:首 上 2 3 4 5 6 下 尾
public class Pager {
// public static void main(String args[]){
// Pager p = new Pager();
// String pstr = p.getInitPager(10,200,1);
// System.err.println("pstr="+pstr);
// }
// private static int pageSize = 20;
/*
* @method 分页,默认显示5页
* */
public static String getInitPager(int pageSize,int totalCount,int curPage){
curPage = (curPage == 0) ? 1:curPage;
int totalPage = getPageCount(pageSize,totalCount);
int p_pre = (curPage>1) ? curPage-1 : 1;//上一页
int p_next = (curPage<totalCount) ? curPage+1 : totalCount;//下一页
//分页首端 固定部分
StringBuffer frontFixedPage = new StringBuffer();
frontFixedPage.append("<ul class='pagination pull-right'><li><a href='javascript:fn_page(1)'>首</a></li>
");
frontFixedPage.append("<li><a href='javascript:fn_page("+p_pre+")'>上</a></li>
");
//分页末尾 固定部分
StringBuffer tailFixedPage = new StringBuffer();
tailFixedPage.append("<li><a href='javascript:fn_page("+p_next+")'>下</a></li>
");
tailFixedPage.append("<li><a href='javascript:fn_page("+totalPage+")'>尾</a></li>
");
//分页末尾统计 固定部分
StringBuffer fianlFixedPage = new StringBuffer();
fianlFixedPage.append("<li class='disabled'><a>页次:"+curPage+"/"+totalPage+"页 共"+totalCount+"条</a></li></ul>");
//分页中间 变动部分
StringBuffer midChangePage = new StringBuffer();
if(totalPage <= 5 ){
for(int i = 1; i <= totalPage; i++){
String active = (curPage == i) ? "class='active'" : "";
midChangePage.append("<li "+active+"><a href='javascript:fn_page("+i+")'>"+i+"</a></li>
");
}
}else{
//页数n>5的,显示页数n-2,n-1,n,n+1,n+2
if(curPage > 2 && curPage <= totalPage - 2){
for(int i = curPage-2; i <= curPage + 2; i++){
String active = (curPage == i) ? "class='active'" : "";
midChangePage.append("<li "+active+"><a href='javascript:fn_page("+i+")'>"+i+"</a></li>
");
}
}
//当前页<=2的,显示1,2,3,4,5
else if(curPage<=2){
for(int i = 1; i <= 5; i++){
String active = (curPage == i) ? "class='active'" : "";
midChangePage.append("<li "+active+"><a href='javascript:fn_page("+i+")'>"+i+"</a></li>
");
}
}
//当前页>末尾第二页的,显示n-4,n-3,n-2,n-1,n
else if(curPage > totalPage - 2){
for(int i = totalPage - 4; i <= totalPage; i++){
String active = (curPage == i) ? "class='active'" : "";
midChangePage.append("<li "+active+"><a href='javascript:fn_page("+i+")'>"+i+"</a></li>
");
}
}
}
StringBuffer pageStr = new StringBuffer();
pageStr.append(frontFixedPage);
pageStr.append(midChangePage);
pageStr.append(tailFixedPage);
pageStr.append(fianlFixedPage);
frontFixedPage = null;
midChangePage = null;
tailFixedPage = null;
fianlFixedPage = null;
return pageStr.toString();
}
/*
* @method 计算总页数
* */
public static int getPageCount(int pageSize,int totalCount){
int p = (int)Math.ceil((double)totalCount/pageSize);
p = (p == 0) ? 1 : p;
return p;
}
/*<ul class='pagination pull-right'>
<li>
<a href='javascript:fn_page(1)'>首</a></li>
<li>
<a href='javascript:fn_page(1)'>上</a></li>
<li class='active'>
<a href='javascript:'>1</a></li>
<li>
<a href='javascript:fn_page(2)'>2</a></li>
<li>
<a href='javascript:fn_page(3)'>3</a></li>
<li><a href='javascript:fn_page(2)'>下</a></li>
<li><a href='javascript:fn_page(3)'>尾</a></li>
<li class='disabled'>
<a>页次:1/3页 共26条</a>
</li>
</ul>*/
}