- /**
- *
- */
- package com.dotease.common;
- import java.util.List;
- /**
- * @author Administrator牛一Niuyi 2007-11-9
- *2007-11-9
- */
- public class Pager
- {
- private int curPage;
- private int lastPage;
- private int prevPage;
- private int nextPage;
- private int resultCount;
- private int pageSize;
- private int pageCount;
- private String uri; //=uri+?+queryString
- private String pageInfo="";
- private List data;
- public Pager()
- {
- this.pageSize=10;
- }
- public Pager(int curPage, int pageSize,int resultCount, String uri)
- {
- this.curPage = curPage;
- this.resultCount = resultCount;
- this.pageSize = pageSize;
- this.uri = uri;
- }
- public String getFirstPageTag()
- {
- if(0==getPageCount())
- {
- return "<FONT face=Webdings>9</FONT>";
- }
- StringBuffer sb = new StringBuffer();
- sb.append("<A title=第一页 href=/""+this.getUri()+"1/">");
- sb.append("<FONT face=Webdings>9</FONT></A>");
- return sb.toString();
- }
- public String getCurPageTag()
- {
- if(0==getPageCount())
- return "";
- StringBuffer sb = new StringBuffer();
- sb.append(" <A title=当前页 href=/""+this.getUri()+this.getCurPage()+"/">");
- sb.append("<FONT color=red><STRONG>");
- sb.append(this.getCurPage()+"</STRONG></FONT></A> ");
- return sb.toString();
- }
- public String getLastPageTag()
- {
- if(0==getPageCount())
- {
- return "<FONT face=Webdings>:</FONT>";
- }
- StringBuffer sb = new StringBuffer();
- sb.append("<A title=最后一页 href=/""+this.getUri()+this.getLastPage()+"/">");
- sb.append("<FONT face=Webdings>:</FONT></A>");
- return sb.toString();
- }
- public String getPrevPageTag()
- {
- if(curPage==1||0==getPageCount())
- {
- return "<FONT face=Webdings>7</FONT>";
- }
- StringBuffer sb = new StringBuffer();
- sb.append("<A title=上一页 href=/""+this.getUri()+this.getPrevPage()+"/">");
- sb.append("<FONT face=Webdings>7</FONT></A>");
- return sb.toString();
- }
- public String getNextPageTag()
- {
- if(curPage==getPageCount()||0==getPageCount())
- {
- return "<FONT face=Webdings>8</FONT>";
- }
- StringBuffer sb = new StringBuffer();
- sb.append("<A title=下一页 href=/""+this.getUri()+this.getNextPage()+"/">");
- sb.append("<FONT face=Webdings>8</FONT></A>");
- return sb.toString();
- }
- //commonTag
- public String getcommonTag(int index)
- {
- return " <A href=/""+this.getUri()+index+"/">"+index+"</A>";
- }
- 获得页码列表
- public String getListNumTag()
- {
- int num=0;//要显示的页码数
- int startNum = 0;//从那个数开始
- int pageCount=this.getPageCount();
- if(pageCount<10)
- {
- num=this.getPageCount();
- }else
- {
- startNum=((curPage-1)/10)*10;
- if((startNum+10)>pageCount)
- {
- num=pageCount-startNum;
- }else
- {
- num=10;
- }
- }
- StringBuffer sb = new StringBuffer() ;
- for(int i=1;i<=num;i++)
- {
- if(curPage==(startNum+i))
- {
- sb.append(this.getCurPageTag());
- }else
- {
- sb.append(getcommonTag(startNum+i));
- }
- }
- return sb.toString();
- }
- //获得页码列表的显示信息
- public String getPageInfo()
- {
- StringBuffer sb = new StringBuffer() ;
- int pageCount=this.getPageCount();
- sb.append("共检索到");
- sb.append("<STRONG>"+resultCount+"</STRONG>记录 ");
- sb.append("当前:第<STRONG><FONT color=red>"+curPage+"</FONT>/"+pageCount+"</STRONG>页 ");
- sb.append("每页<STRONG>"+pageSize+"</STRONG>条记录 ");
- sb.append(this.getFirstPageTag());
- sb.append(this.getPrevPageTag());
- sb.append(this.getListNumTag());
- sb.append(this.getNextPageTag());
- sb.append(this.getLastPageTag());
- sb.append("转到"+this.getSelect());
- return sb.toString();
- }
- public String getSelect()
- {
- StringBuffer sb = new StringBuffer();
- sb.append("<SELECT οnchange=window.location.href=/""+uri+"/"+this.value name=page_sel>");
- int pageCount=this.getPageCount();
- for(int n=1;n<=pageCount;n++)
- {
- if (n==curPage)
- {
- sb.append("<OPTION selected value="+n+">"+n+"</OPTION>");
- }else
- {
- sb.append("<OPTION value="+n+">"+n+"</OPTION>");
- }
- }
- sb.append("</SELECT>");
- return sb.toString();
- }
- public String getSkipPage()
- {
- StringBuffer sb = new StringBuffer();
- sb.append("<script>");
- sb.append("/nfunction goPage(pageNumber) {");
- sb.append("/nwindow.location.href=/"" + getUri() + "/" + pageNumber;");
- sb.append("/n}");
- sb.append("/n</script>/n");
- sb.append("<input name=/"page_num/" type=/"text/" onBlur=/"goPage(this.value)/" size=/"3/" maxlength=/"3/">");
- return sb.toString();
- }
- public int getCurPage() {
- return curPage;
- }
- public void setCurPage(int curPage) {
- this.curPage = curPage;
- }
- public int getFirstPage() {
- return 1;
- }
- public int getLastPage() {
- return this.getPageCount();
- }
- public int getNextPage()
- {
- if (curPage == getPageCount())
- {
- return curPage;
- }
- return curPage + 1;
- }
- public int getPageCount()
- {
- if(resultCount==0)
- {
- this.curPage=0;
- return 0;
- }
- pageCount=(resultCount - 1) / pageSize + 1;
- if(curPage>pageCount)
- curPage=pageCount;
- return pageCount;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getPrevPage()
- {
- if (1 == curPage)
- {
- return 1;
- }
- return curPage - 1;
- }
- public int getResultCount() {
- return resultCount;
- }
- public void setResultCount(int resultCount)
- {
- this.resultCount = resultCount;
- }
- public String getUri() {
- return uri;
- }
- public void setUri(String uri)
- {
- int p=uri.lastIndexOf("curPage=");
- if(p==-1)
- {
- uri=uri+"&curPage=";
- }else
- {
- uri=uri.substring(0,p+8);
- }
- this.uri = uri;
- }
- //aa
- public List getData() {
- return data;
- }
- public void setData(List data) {
- this.data = data;
- }
- }
/** * */ package com.dotease.common; import java.util.List; /** * @author Administrator牛一Niuyi 2007-11-9 *2007-11-9 */ public class Pager { private int curPage; private int lastPage; private int prevPage; private int nextPage; private int resultCount; private int pageSize; private int pageCount; private String uri; //=uri+?+queryString private String pageInfo=""; private List data; public Pager() { this.pageSize=10; } public Pager(int curPage, int pageSize,int resultCount, String uri) { this.curPage = curPage; this.resultCount = resultCount; this.pageSize = pageSize; this.uri = uri; } public String getFirstPageTag() { if(0==getPageCount()) { return "<FONT face=Webdings>9</FONT>"; } StringBuffer sb = new StringBuffer(); sb.append("<A title=第一页 href=/""+this.getUri()+"1/">"); sb.append("<FONT face=Webdings>9</FONT></A>"); return sb.toString(); } public String getCurPageTag() { if(0==getPageCount()) return ""; StringBuffer sb = new StringBuffer(); sb.append(" <A title=当前页 href=/""+this.getUri()+this.getCurPage()+"/">"); sb.append("<FONT color=red><STRONG>"); sb.append(this.getCurPage()+"</STRONG></FONT></A> "); return sb.toString(); } public String getLastPageTag() { if(0==getPageCount()) { return "<FONT face=Webdings>:</FONT>"; } StringBuffer sb = new StringBuffer(); sb.append("<A title=最后一页 href=/""+this.getUri()+this.getLastPage()+"/">"); sb.append("<FONT face=Webdings>:</FONT></A>"); return sb.toString(); } public String getPrevPageTag() { if(curPage==1||0==getPageCount()) { return "<FONT face=Webdings>7</FONT>"; } StringBuffer sb = new StringBuffer(); sb.append("<A title=上一页 href=/""+this.getUri()+this.getPrevPage()+"/">"); sb.append("<FONT face=Webdings>7</FONT></A>"); return sb.toString(); } public String getNextPageTag() { if(curPage==getPageCount()||0==getPageCount()) { return "<FONT face=Webdings>8</FONT>"; } StringBuffer sb = new StringBuffer(); sb.append("<A title=下一页 href=/""+this.getUri()+this.getNextPage()+"/">"); sb.append("<FONT face=Webdings>8</FONT></A>"); return sb.toString(); } //commonTag public String getcommonTag(int index) { return " <A href=/""+this.getUri()+index+"/">"+index+"</A>"; } 获得页码列表 public String getListNumTag() { int num=0;//要显示的页码数 int startNum = 0;//从那个数开始 int pageCount=this.getPageCount(); if(pageCount<10) { num=this.getPageCount(); }else { startNum=((curPage-1)/10)*10; if((startNum+10)>pageCount) { num=pageCount-startNum; }else { num=10; } } StringBuffer sb = new StringBuffer() ; for(int i=1;i<=num;i++) { if(curPage==(startNum+i)) { sb.append(this.getCurPageTag()); }else { sb.append(getcommonTag(startNum+i)); } } return sb.toString(); } //获得页码列表的显示信息 public String getPageInfo() { StringBuffer sb = new StringBuffer() ; int pageCount=this.getPageCount(); sb.append("共检索到"); sb.append("<STRONG>"+resultCount+"</STRONG>记录 "); sb.append("当前:第<STRONG><FONT color=red>"+curPage+"</FONT>/"+pageCount+"</STRONG>页 "); sb.append("每页<STRONG>"+pageSize+"</STRONG>条记录 "); sb.append(this.getFirstPageTag()); sb.append(this.getPrevPageTag()); sb.append(this.getListNumTag()); sb.append(this.getNextPageTag()); sb.append(this.getLastPageTag()); sb.append("转到"+this.getSelect()); return sb.toString(); } public String getSelect() { StringBuffer sb = new StringBuffer(); sb.append("<SELECT οnchange=window.location.href=/""+uri+"/"+this.value name=page_sel>"); int pageCount=this.getPageCount(); for(int n=1;n<=pageCount;n++) { if (n==curPage) { sb.append("<OPTION selected value="+n+">"+n+"</OPTION>"); }else { sb.append("<OPTION value="+n+">"+n+"</OPTION>"); } } sb.append("</SELECT>"); return sb.toString(); } public String getSkipPage() { StringBuffer sb = new StringBuffer(); sb.append("<script>"); sb.append("/nfunction goPage(pageNumber) {"); sb.append("/nwindow.location.href=/"" + getUri() + "/" + pageNumber;"); sb.append("/n}"); sb.append("/n</script>/n"); sb.append("<input name=/"page_num/" type=/"text/" onBlur=/"goPage(this.value)/" size=/"3/" maxlength=/"3/">"); return sb.toString(); } public int getCurPage() { return curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public int getFirstPage() { return 1; } public int getLastPage() { return this.getPageCount(); } public int getNextPage() { if (curPage == getPageCount()) { return curPage; } return curPage + 1; } public int getPageCount() { if(resultCount==0) { this.curPage=0; return 0; } pageCount=(resultCount - 1) / pageSize + 1; if(curPage>pageCount) curPage=pageCount; return pageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPrevPage() { if (1 == curPage) { return 1; } return curPage - 1; } public int getResultCount() { return resultCount; } public void setResultCount(int resultCount) { this.resultCount = resultCount; } public String getUri() { return uri; } public void setUri(String uri) { int p=uri.lastIndexOf("curPage="); if(p==-1) { uri=uri+"&curPage="; }else { uri=uri.substring(0,p+8); } this.uri = uri; } //aa public List getData() { return data; } public void setData(List data) { this.data = data; } }
uri就是url !
以上为封装分页信息的Java类!
使用代码
//设置数据
<%
pager.setUri(request.getRequestURI()+"?"+request.getQueryString());
pager.setCurPage(curPage);
pager.setPageSize(pageSize);
pager.setData(bookList);
pager.setResultCount(count);
request.setAttribute("pager",pager);
%>
//若想显示页码列表和一个页码下拉框用${pager.pageInfo}
//若要加一个跳转页码的文本框用${pager.skipPage}
使用${pager.pageInfo}效果如图: