一個(gè)簡(jiǎn)單的java分頁(yè)小模塊
public class Page {
public static void main(String[] args) {
Page pg=new Page();
String str=pg.pagestr("http://www.abc.com?",195,11,10,8);
System.out.print(str);
}
//url連接,countdatas表數(shù)據(jù)總行數(shù)谋竖,currentpage第幾頁(yè)(0是第一頁(yè)),everpage每頁(yè)多少行,beforlaterln前后長(zhǎng)度
public String pagestr(String url,int countdatas,int currentpage,int everpage,int beforlaterln) {
String page= "";
int intpart = countdatas/everpage;//整數(shù)
int remainderpart = countdatas%everpage;//余數(shù)
int countpage = intpart;//取整數(shù)
currentpage = currentpage<0 ? 0:currentpage;
if(remainderpart==0){//余數(shù)判斷
currentpage=currentpage>countpage-1?countpage-1:currentpage;
int pp=currentpage+1;
String strbe="";
int pagebefore=pp-beforlaterln-1;
for(int i=pp; i > pagebefore; i--){
if(i>0 && i<pp){
strbe="<a class='num' href='"+url+"&p="+(i-1)+"'>"+i+"</a>"+strbe;
}
}
if(pp<=1){strbe="";}else{strbe="<a class='num' href='"+url+"&p=0'>首頁(yè)</a>"+"<a class='num' href='"+url+"&p="+(pp-2)+"'>上一頁(yè)</a>"+strbe;}
String strmid="<span class='current'>"+pp+"</span>";
String strlater="";
int pagelater=pp+beforlaterln;
for(int i=pp; i < pagelater; i++){
if(i<countpage){
strlater+="<a class='num' href='"+url+"&p="+(i)+"'>"+(i+1)+"</a>";
}
}
if(pp>=countpage){}else{strlater+="<a class='num' href='"+url+"&p="+pp+"'>下一頁(yè)</a>"+"<a class='num' href='"+url+"&p="+(countpage-1)+"'>尾頁(yè)</a>";}
page=strbe+strmid+strlater;
}else {
countpage+=1;//總頁(yè)數(shù)
currentpage=currentpage>countpage-1?countpage-1:currentpage;
int pp=currentpage+1;
String strbe="";
int pagebefore=pp-beforlaterln-1;
for(int i=pp; i > pagebefore; i--){
if(i>0 && i<pp){
strbe="<a class='num' href='"+url+"&p="+(i-1)+"'>"+i+"</a>"+strbe;
}
}
if(pp<=1){strbe="";}else{strbe="<a class='num' href='"+url+"&p=0'>首頁(yè)</a>"+"<a class='num' href='"+url+"&p="+(pp-2)+"'>上一頁(yè)</a>"+strbe;}
String strmid="<span class='current'>"+pp+"</span>";
String strlater="";
int pagelater=pp+beforlaterln;
for(int i=pp; i < pagelater; i++){
if(i<countpage){
strlater+="<a class='num' href='"+url+"&p="+(i)+"'>"+(i+1)+"</a>";
}
}
if(pp>=countpage){}else{strlater+="<a class='num' href='"+url+"&p="+pp+"'>下一頁(yè)</a>"+"<a class='num' href='"+url+"&p="+(countpage-1)+"'>尾頁(yè)</a>";}
page=strbe+strmid+strlater;
}
return "共"+countpage+"頁(yè)"+page;
}
}
運(yùn)行后得到以下字符串
共20頁(yè)<a class='num' >首頁(yè)</a>
<a class='num' >上一頁(yè)</a>
<a class='num' >4</a>
<a class='num' >5</a>
<a class='num' >6</a>
<a class='num' >7</a>
<a class='num' >8</a>
<a class='num' >9</a>
<a class='num' >10</a>
<a class='num' >11</a>
<span class='current'>12</span>
<a class='num' >13</a>
<a class='num' >14</a>
<a class='num' >15</a>
<a class='num' >16</a>
<a class='num' >17</a>
<a class='num' >18</a>
<a class='num' >19</a>
<a class='num' >20</a>
<a class='num' >下一頁(yè)</a>
<a class='num' >尾頁(yè)</a>
希望對(duì)大家有幫助缝龄,謝謝旺韭!