java web 通用的分页组件.docx
- 文档编号:25395030
- 上传时间:2023-06-08
- 格式:DOCX
- 页数:34
- 大小:51.68KB
java web 通用的分页组件.docx
《java web 通用的分页组件.docx》由会员分享,可在线阅读,更多相关《java web 通用的分页组件.docx(34页珍藏版)》请在冰豆网上搜索。
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;
privateMap
privateList
privateStringvalue="";
privateStringtext="";
publicHtmlElement(StringtagName){
this.tagName=tagName;
}
publicvoidsetValue(Stringvalue){
this.value=value;
}
publicList
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
for(Entry
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); }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java web 通用的分页组件 通用 分页 组件