分页组件

08-01-18 brucehu
我是菜鸟级别的,大家一定要指出缺点来,我也没写过什么项目,只是觉得写这么个类,对一些分页的程序有些帮助,大家使劲批评就行,呵呵

,字符串连接一般用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修改过]

    

banq
2008-01-21 16:45
做得不出,目前看不出问题,但是这可能是多层架构下分页的一个小部分,数据库如何分页导出也是很重要的内容;Jsp如何在不写入Java代码情况下显示分页信息也是一个方面。

sgysgy
2008-01-23 10:17
凑个热闹,我的分页:

InPage.java

public interface InPage {

//当前页

public void setCurrentPage(int currentPage);

//每页条目数

public void setEveryItems(int everyItems);

//总记录数

public void setAllItems(int allItems);

// 总页数

public void setAllPage(int allpage);

//当前页

public int getCurrentPage();

//每页条目数

public int getEveryItems();

//总记录数

public int getAllItems();

//总页数

public int getAllPage();

//是否有下一页

public boolean hasNextPage();

//是否有上一页

public boolean hasPreviousPage();

//是否有尾页

public boolean hasLastPage();

//是否有首页

public boolean hasFirstPage();

//记录集

public Object getDataValues();

//记录集

public void setDataValues(Object dataValues);

//分页条

public String showPageBar(String url);

}

==================================================

PageBean.java

public class PageBean implements InPage{

//记录集

private Object dataValues;

//当前页

private int currentPage;

//每页记录

private int everyItems;

//总记录

private int allItems;

//总页数

private int allPage;

/**是否有最后一页*/

public boolean hasLastPage() {

return currentPage<getAllPage();

}

/**是否有下一页*/

public boolean hasNextPage() {

return currentPage<getAllPage();

}

/**是否有首页*/

public boolean hasFirstPage() {

return currentPage>1;

}

/**是否有上一页*/

public boolean hasPreviousPage() {

return currentPage>1;

}

public void setAllItems(int allItems) {

this.allItems=allItems;

}

public void setCurrentPage(int currentPage) {

this.currentPage=currentPage;

}

public void setEveryItems(int everyItems) {

this.everyItems=everyItems;

}

public int getAllItems() {

return allItems;

}

public int getCurrentPage() {

return currentPage;

}

public int getEveryItems() {

return everyItems;

}

public int getAllPage() {

return this.allItems%this.everyItems==0?this.allItems/this.everyItems:this.allItems/this.everyItems+1;

}

public void setAllPage(int allpage) {

this.allPage=this.allItems%this.everyItems==0?this.allItems/this.everyItems:this.allItems/this.everyItems+1;

}

public Object getDataValues() {

return dataValues;

}

public void setDataValues(Object dataValues) {

this.dataValues = dataValues;

}

public String showPageBar(String url) {

StringBuffer sb = new StringBuffer();

String strFirstPage = "首页";

String strPrevPage = "上一页";

String strNextPage = "下一页";

String strLastPage = "末页";

sb.append("<form name=myform action=" + url + " method=POST> ");

sb.append("当前第"+currentPage+"/"+getAllPage()+"页  ");

if(hasFirstPage()){

sb.append("<a href="+url+"¤tPage=1&everyItems="+everyItems+">"+strFirstPage+"</a>  ");

}else{

sb.append(strFirstPage+"  ");

}

if(hasPreviousPage()){

sb.append("<a href="+url+"¤tPage="+(currentPage-1)+"&everyItems="+everyItems+">"+strPrevPage+"</a>  ");

}else{

sb.append(strPrevPage+"  ");

}

if(hasNextPage()){

sb.append("<a href="+url+"¤tPage="+(currentPage+1)+"&everyItems="+everyItems+">"+strNextPage+"</a>  ");

}else{

sb.append(strNextPage+"  ");

}

if(hasLastPage()){

sb.append("<a href="+url+"¤tPage="+getAllPage()+"&everyItems="+everyItems+">"+strLastPage+"</a>  ");

}else{

sb.append(strLastPage+"  ");

}

sb.append("转至第<input type=text name=currentPage size=4>页<input type=hidden name=everyItems value="+everyItems+">");

sb.append("<input class=button type=submit value=Go></form>");

return sb.toString();

}

}

======================

放到Request里面

页面写代码获取InPage,InPage.getDataValues()是记录集

InPage.showPageBar()是导航栏,根据需要自己改

[该贴被sgysgy于2008-01-23 10:18修改过]

猜你喜欢