packageexamples;/** @(#)StringPager.java 1.00 2004-10-10
*
* Copyright 2004 . All rights reserved.
* PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.*//*** StringPager, 字符串分页.
*
*@authorxxxxxx
*@version1.00 2004-10-10*/publicclassStringPager {/**当前页码数*/privateintcurrentPage=1;/**需要分页的长字符串*/privateString text;/**每页显示字符数, 默认为 200*/privateintsizePerPage=200;/**总页数*/privateinttotalPage;publicStringPager() {}/*** 返回当前页的字符串.
*@return*/publicString getCurrentPagedText() {try{if(getCurrentPage()
getCurrentPage()*getSizePerPage());
}elseif(getTotalPage()>0) {returngetText().substring((getCurrentPage()-1)*getSizePerPage());
}
}catch(Exception e) {
e.printStackTrace();
}returnnull;
}/***@returnReturns the 当前页码数.*/publicintgetCurrentPage() {if(currentPage<=0) currentPage=1;returncurrentPage;
}/*** 设置当前页码, 从 1 开始.
*
*@paramcurrentPage
* The 当前页码数 to set.*/publicvoidsetCurrentPage(intcurrentPage) {if(currentPage<=0) {
currentPage=1;
}this.currentPage=currentPage;
}/***@returnReturns the 总页码数, 如果没有数据, 就返回 1.*/publicintgetTotalPage() {if(getText()==null) totalPage=0;
totalPage=(int) Math.ceil(1.0*getText().length()/getSizePerPage());//总页面数if(totalPage==0)
totalPage=1;returntotalPage;
}/***@paramtotalPage
* The totalPage to set.*/publicvoidsetTotalPage(inttotalPage) {this.totalPage=totalPage;
}publicstaticvoidmain(String[] args) {
StringPager pager=newStringPager();
pager.setSizePerPage(5);
pager.setText("12345678901");
pager.setCurrentPage(2);
System.out.println(pager.getTotalPage());
System.out.println(pager.getCurrentPagedText());
}/***@returnReturns the 每页显示字符数.*/publicintgetSizePerPage() {returnsizePerPage;
}/***@paramsizePerPage
* The 每页显示字符数 to set.*/publicvoidsetSizePerPage(intsizePerPage) {this.sizePerPage=sizePerPage;
}/***@returnReturns the 需要分页的长字符串.*/publicString getText() {returntext;
}/***@paramtext
* The 需要分页的长字符串 to set.*/publicvoidsetText(String text) {this.text=text;
}
}
document.PageForm.submit();return;
}
function gotoPage(pagenum){
document.PageForm.jumpPage.value=pagenum;
document.PageForm.submit();return;
}
pager.setText("12345678wfasdfae3fgsffd34235534634gfsfd65sfgssaeweasdfa901");//需要分页的长文本pager.setSizePerPage(5);//每页显示字符数intcurPage;//当前是第几页intmaxPage;//一共有多少页curPage=1;//当前页maxPage=pager.getTotalPage();
String jumpPage=(String)request.getParameter("jumpPage");if(jumpPage!=null&&!jumpPage.equals("")){
curPage=Integer.parseInt(jumpPage);
}
pager.setCurrentPage(curPage);
out.println("第"+curPage+"页
");
out.println(pager.getCurrentPagedText());//当前页的字符串%>
每页字第页
共页首页)">上一页)">下一页)">尾页转到第>>页
第二种方法:
packagecom.accp.Bean;importjava.util.*;publicclassStringPager {/**当前页码数*/privateintcurrentPage=1;/**每页显示行数, 默认为 200*/privateintsizePerPage=5;/**总页数*/privateinttotalPage;/**需要分页的长字符串*/privateList topicSelect;publicStringPager() {
}/*** 返回当前页的文本
*
*@return*/publicList getCurrentPagedText() {try{if(getCurrentPage()
(getCurrentPage()-1)*getSizePerPage(),getCurrentPage()*getSizePerPage());
}elseif(getTotalPage()>0) {returngetTopicSelect().subList((getCurrentPage()-1)*getSizePerPage(),getTopicSelect().size());
}
}catch(Exception e) {
e.printStackTrace();
}returnnull;
}/***@returnReturns the 当前页码数.*/publicintgetCurrentPage() {if(currentPage<=0)
currentPage=1;returncurrentPage;
}/*** 设置当前页码, 从 1 开始.
*
*@paramcurrentPage
* The 当前页码数 to set.*/publicvoidsetCurrentPage(intcurrentPage) {if(currentPage<=0) {
currentPage=1;
}this.currentPage=currentPage;
}/***@returnReturns the 总页码数, 如果没有数据, 就返回 1.*/publicintgetTotalPage() {if(getTopicSelect()==null)
totalPage=0;
totalPage=(int) Math.ceil(1.0*getTopicSelect().size()/getSizePerPage());//总页面数if(totalPage==0)
totalPage=1;returntotalPage;
}/***@paramtotalPage
* The totalPage to set.*/publicvoidsetTotalPage(inttotalPage) {this.totalPage=totalPage;
}/***@returnReturns the 每页显示行数.*/publicintgetSizePerPage() {returnsizePerPage;
}/***@paramsizePerPage
* The 每页显示行数to set.*/publicvoidsetSizePerPage(intsizePerPage) {this.sizePerPage=sizePerPage;
}/***@returnReturns the 需要分页的文本.*/publicList getTopicSelect() {returntopicSelect;
}publicvoidsetTopicSelect(List topicSelect) {this.topicSelect=topicSelect;
}
}
jsp代码
A{text-decoration:none;}
A:hover{color:blue}
Insert title hereTopicDao dao=newTopicDao();
List lst=dao.selectAll();
pager.setTopicSelect(lst);//需要分页的文本pager.setSizePerPage(5);//每页显示行数intcurPage;//当前是第几页intmaxPage;//一共有多少页curPage=1;//当前页maxPage=pager.getTotalPage();
String jumpPage=(String)request.getParameter("jumpPage");if(jumpPage!=null&&!jumpPage.equals("")){
curPage=Integer.parseInt(jumpPage);
}
pager.setCurrentPage(curPage);%>
欢迎进入帖子管理页面
每页条 第页 共页 | out.print("首页 上一页"); }else{%>首页)">上一页 out.print("下一页 尾页"); }else{%>)">下一页)">尾页转到第>>页 | |||
序号 | 标题 | 内容 | 积分 | 等级 |
Topic topic=(Topic)(pager.getCurrentPagedText().get(i));%>
}if(str.length()>8){returnstr.substring(0,8)+"";
}else{returnstr;
}
}%>