java web 通用的分页组件.docx

上传人:b****9 文档编号:25395030 上传时间:2023-06-08 格式:DOCX 页数:34 大小:51.68KB
下载 相关 举报
java web 通用的分页组件.docx_第1页
第1页 / 共34页
java web 通用的分页组件.docx_第2页
第2页 / 共34页
java web 通用的分页组件.docx_第3页
第3页 / 共34页
java web 通用的分页组件.docx_第4页
第4页 / 共34页
java web 通用的分页组件.docx_第5页
第5页 / 共34页
点击查看更多>>
下载资源
资源描述

java web 通用的分页组件.docx

《java web 通用的分页组件.docx》由会员分享,可在线阅读,更多相关《java web 通用的分页组件.docx(34页珍藏版)》请在冰豆网上搜索。

java web 通用的分页组件.docx

javaweb通用的分页组件

  CSDN博客专栏重磅推出,汇集精华系列博文!

                  提交原创APP,20万等你赢

【Android编程之旅】社区活动:

写博文获精彩图书!

                          

javaweb通用的分页组件

2008-07-2700:

092820人阅读评论(9)收藏举报

通用的分页组件

有时候找到一个合适的web表格分页组件真是一件痛苦的事情,要么是没有完全封装的分页方法,使用起来比较麻烦,不够方便,要么使用方便,但很难同项目结合,也有许多开源分页组件,功能非常强大,但使用起来有很多问题,有些不好与系统结合,有些不好扩展,但总感觉有些浮肿,很多功能不够实用,也没多大意义,就此尝试写了一个分页组件,够用了,但也有不少问题,望大家共同修正.

1、 HTML元素java封装:

为了操作方便起见,

packagecommons.page;

 

importjava.util.ArrayList;

importjava.util.HashMap;

importjava.util.List;

importjava.util.Map;

importjava.util.Set;

importjava.util.Map.Entry;

 

publicclassHtmlElement{

      privateStringtagName;

      privateMapattributes=newHashMap();

      privateListchildren=newArrayList();

      privateStringvalue="";

      privateStringtext="";

 

      publicHtmlElement(StringtagName){

             this.tagName=tagName;

      }

 

      publicvoidsetValue(Stringvalue){

             this.value=value;

      }

 

      publicListgetChildren(){

             returnchildren;

      }

 

      publicvoidaddChild(HtmlElementelement){

             this.children.add(element);

      }

 

      publicvoidaddAttribute(Stringname,Stringvalue){

             this.attributes.put(name,value);

      }

 

      publicStringgetAttributeValue(Stringname){

             returnthis.attributes.get(name);

      }

      @Override

      publicStringtoString(){

             returnthis.toHtml();

      }

      publicStringtoHtml(){

             StringBuffersb=newStringBuffer();

             sb.append("<");

             sb.append(this.tagName);

             sb.append(this.getAttr());

             sb.append(">");

             sb.append(this.value);

             for(HtmlElemente:

this.children){

                    sb.append(e.toHtml());

             }

             sb.append(this.text);

             sb.append("

             sb.append(this.tagName);

             sb.append(">");

             returnsb.toString();

      }

      protectedStringgetAttr(){

             StringBuffersb=newStringBuffer();

             Set>attr=this.attributes.entrySet();

             for(Entryentry:

attr){

                    sb.append("");

                    sb.append(entry.getKey());

                    sb.append("=/"");

                    sb.append(entry.getValue());

                    sb.append("/"");

             }

             returnsb.toString();

      }

 

      publicvoidsetText(Stringtext){

             this.text=text;

      }

}

      例如:

HtmlElementdiv1=newHtmlElement("div");

             HtmlElementdiv2=newHtmlElement("div");

             HtmlElementspan=newHtmlElement("span");

             div1.addAttribute("class","tuo");

             div1.addAttribute("id","dd1");

             div1.addChild(div2);

             div1.addChild(span);

             div1.setValue("fds222222fds");

             div1.setText("wwwwwwwww");

       System.out.println(div1.toHtml());

生成:

fds222222fds

Wwwwwwwww

 

2、 

 

 

 

 

 

 

 

 

数据表格类

packagecommons.page;

 

importjava.util.Calendar;

importjava.util.List;

 

importcommons.page.model.ArrayDataModel;

importcommons.page.model.DataModel;

importcommons.page.model.ListDataModel;

 

publicclassHtmlTableextendsHtmlElement{

      privateHtmlElementthead=newHtmlElement("thead");

      privateHtmlElementtbody=newHtmlElement("tbody");

      privateHtmlElementtfoot=newHtmlElement("tfoot");

      privateString[]header=null;

      privateString[]footer=null;

      privateDataModeldataModel=null;

      privateintstartRowIndex=-1;//开始行索引

      privateintmaxRowNum=-1;//最大行数

      privateCellCallBackcallBack;

 

      publicHtmlTable(){

             super("table");

      }

      publicvoidsetCellCallBack(CellCallBackcallBack){

             this.callBack=callBack;

      }

      publicvoidsetMaxRowNum(intmaxRowNum){

             this.maxRowNum=maxRowNum;

      }

      publicvoidsetStartRowIndex(intstartRowIndex){

             this.startRowIndex=startRowIndex;

      }

      publicvoidsetHeader(String[]header){

             this.header=header;

      }

      publicvoidsetHeader(Stringheader){

             this.header=header.split(",");

      }

      publicvoidsetFooter(String[]footer){

             this.footer=footer;

      }

      publicvoidsetFooter(Stringfooter){

             this.footer=footer.split(",");

      }

      publicvoidsetDataModel(DataModeldataModel){

             this.dataModel=dataModel;

      }

      publicvoidsetData(Object[][]data){

             this.dataModel=newArrayDataModel();

             this.dataModel.setWrappedData(data);

      }

      publicvoidsetData(Listdata){

             this.dataModel=newListDataModel();

             this.dataModel.setWrappedData(data);

      }

 

      publicvoidsetTbody(HtmlElementtbody){

             this.tbody=tbody;

      }

 

      publicvoidsetTfoot(HtmlElementtfoot){

             this.tfoot=tfoot;

      }

 

      publicvoidsetThead(HtmlElementthead){

             this.thead=thead;

      }

      

      @Override

      publicStringtoHtml(){

             Stringid=this.getAttributeValue("id");

             if(id==null||"".equals(id)){

                    this.addAttribute("id","table"+Calendar.getInstance().getTimeInMillis());

             }

             this.process();

 

             returnsuper.toHtml();

      }

      privatevoidprocess(){

             if(this.header!

=null){

                    this.thead.getChildren().clear();

                    HtmlElementtr=newHtmlElement("tr");

                    for(Stringheader:

this.header){

                           HtmlElementth=newHtmlElement("th");

                           th.setText(header);

                           tr.addChild(th);

                    }

                    this.thead.addChild(tr);

                    this.addChild(this.thead);

             }

             if(this.dataModel!

=null){

                    if(this.callBack==null){

                           this.callBack=newCellCallBack(){

                                  publicHtmlElementdoCell(Objectdata){

                                         HtmlRowtr=newHtmlRow();

                                         if(datainstanceofString[]){

                                                String[]ds=(String[])data;

                                                intlen=ds.length;

                                                if(header!

=null){

                                                       if(len>header.length){

                                                              len=header.length;

                                                       }

                                                }

                                                for(inti=0;i

                                                       HtmlElementtd=newHtmlElement("td");

                                                       td.setText(ds[i]==null?

"":

ds[i]);

                                                       tr.addChild(td);

                                                }

                                         }elseif(datainstanceofObject[]){

                                                Object[]os=(Object[])data;

                                                intlen=os.length;

                                                if(header!

=null){

                                                       if(len>header.length){

                                                              len=header.length;

                                                       }

                                                }

                                                for(inti=0;i

                                                       HtmlElementtd=newHtmlElement("td");

                                                       td.setText(os[i]==null?

"":

os[i].toString());

                                                       tr.addChild(td);

                                                }

                                         }

                                         returntr;

                                  }

                           };

                    }

                    intstartRow=0;

                    intendRow=0;

                    intj=this.dataModel.getRowCount();

                    if(this.startRowIndex>0){

                           startRow=this.startRowIndex;

                    }

                    if(this.maxRowNum<0){

                           endRow=this.dataModel.getRowCount();

                    }else{

                           endRow=startRow+this.maxRowNum;

                    }

                    if(endRow>j){

                           endRow=j;

                    }

                    this.tbody.getChildren().clear();

 

                    for(inti=startRow;i

                           this.dataModel.setRowIndex(i);

                           HtmlElementtr=this.callBack.doCell(this.dataModel.getRowData());

                           this.tbody.addChild(tr);

                    }

           

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 教育学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1