分頁元件

brucehu發表於2008-01-18
我是菜鳥級別的,大家一定要指出缺點來,我也沒寫過什麼專案,只是覺得寫這麼個類,對一些分頁的程式有些幫助,大家使勁批評就行,呵呵
,字串連線一般用StringBuffer比較好,我都是用的String的+號連線符
public class PageBean {

private int total;
private int count;
private int begin;
private int end;
private String linkString=null;
private int thispage;
private String link=null;
private String[] parameters=null;

public void makeParameters(){
int yushu=total%count;
int pagecount;
if(yushu==0)
pagecount=total/count;
else
pagecount=(total/count)+1;

if(thispage<=1){
thispage=1;
}else if(thispage>=pagecount){
thispage=pagecount;
}

String firstlink="<a href="+link+"?thispage=1";
String lastlink="<a href="+link+"?thispage="+pagecount;
String prevlink="<a href="+link+"?thispage="+(thispage-1);
String nextlink="<a href="+link+"?thispage="+(thispage+1);
if(parameters!=null){
for(String parameter:parameters){
firstlink+="&"+parameter;
lastlink+="&"+parameter;
prevlink+="&"+parameter;
nextlink+="&"+parameter;
}
}
firstlink+=">第一頁</a>";
lastlink+=">末一頁</a>";
prevlink+=">上一頁</a>";
nextlink+=">下一頁</a>";

linkString=firstlink+"//"+prevlink+"//第"+thispage+"頁/共"+pagecount+"頁//"+nextlink+"//"+lastlink;

begin=(thispage-1)*count+1;

if(begin<=1)

begin=1;
end=begin+count;
//System.out.println("total:"+total+" begin:"+begin+"----end:"+end);
}


public void setCount(int count) {
this.count = count;
}

public void setTotal(int total) {
this.total = total;
}
public int getBegin() {
return begin;
}

public int getEnd() {
return end;
}

public String getLinkstring() {
return linkString;
}

public void setThispage(int thispage) {
this.thispage = thispage;
}


public void setLink(String link) {
this.link = link;
}


public void setParameters(String[] parameters) {
this.parameters = parameters;
}

public static void main(String agers[]){
PageBean page=new PageBean();
page.setLink("view.do");
page.setThispage(1);
page.setCount(20);
page.setTotal(100);
String[] parameters={"phone=1234","linkid=111"};
page.setParameters(parameters);
page.makeParameters();
String pagelink=page.getLinkstring();
System.out.println(pagelink);

System.out.println(page.getBegin());

System.out.println(page.getEnd());
}

}


[該貼被brucehu於2008-01-18 17:36修改過]

相關文章