/**
*
*/
packagecom.dotease.common;
importjava.util.List;
/**
* @author Administrator牛一Niuyi 2007-11-9
*2007-11-9
*/
publicclassPager
{
privateintcurPage;
privateintlastPage;
privateintprevPage;
privateintnextPage;
privateintresultCount;
privateintpageSize;
privateintpageCount;
privateString uri;//=uri+?+queryString
privateString pageInfo="";
privateList data;
publicPager()
{
this.pageSize=10;
}
publicPager(intcurPage,intpageSize,intresultCount, String uri)
{
this.curPage = curPage;
this.resultCount = resultCount;
this.pageSize = pageSize;
this.uri = uri;
}
publicString getFirstPageTag()
{
if(0==getPageCount())
{
return"9";
}
StringBuffer sb =newStringBuffer();
sb.append("");
sb.append("9");
returnsb.toString();
}
publicString getCurPageTag()
{
if(0==getPageCount())
return"";
StringBuffer sb =newStringBuffer();
sb.append(" ");
sb.append("");
sb.append(this.getCurPage()+" ");
returnsb.toString();
}
publicString getLastPageTag()
{
if(0==getPageCount())
{
return":";
}
StringBuffer sb =newStringBuffer();
sb.append("");
sb.append(":");
returnsb.toString();
}
publicString getPrevPageTag()
{
if(curPage==1||0==getPageCount())
{
return"7";
}
StringBuffer sb =newStringBuffer();
sb.append("");
sb.append("7");
returnsb.toString();
}
publicString getNextPageTag()
{
if(curPage==getPageCount()||0==getPageCount())
{
return"8";
}
StringBuffer sb =newStringBuffer();
sb.append("");
sb.append("8");
returnsb.toString();
}
//commonTag
publicString getcommonTag(intindex)
{
return" "+index+"";
}
获得页码列表
publicString getListNumTag()
{
intnum=0;//要显示的页码数
intstartNum =0;//从那个数开始
intpageCount=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 =newStringBuffer() ;
for(inti=1;i<=num;i++)
{
if(curPage==(startNum+i))
{
sb.append(this.getCurPageTag());
}else
{
sb.append(getcommonTag(startNum+i));
}
}
returnsb.toString();
}
//获得页码列表的显示信息
publicString getPageInfo()
{
StringBuffer sb =newStringBuffer() ;
intpageCount=this.getPageCount();
sb.append("共检索到");
sb.append(""+resultCount+"记录 ");
sb.append("当前:第"+curPage+"/"+pageCount+"页 ");
sb.append("每页"+pageSize+"条记录 ");
sb.append(this.getFirstPageTag());
sb.append(this.getPrevPageTag());
sb.append(this.getListNumTag());
sb.append(this.getNextPageTag());
sb.append(this.getLastPageTag());
sb.append("转到"+this.getSelect());
returnsb.toString();
}
publicString getSelect()
{
StringBuffer sb =newStringBuffer();
sb.append("");
intpageCount=this.getPageCount();
for(intn=1;n<=pageCount;n++)
{
if(n==curPage)
{
sb.append(""+n+"");
}else
{
sb.append(""+n+"");
}
}
sb.append("");
returnsb.toString();
}
publicString getSkipPage()
{
StringBuffer sb =newStringBuffer();
sb.append("
sb.append("/nfunction goPage(pageNumber) {");
sb.append("/nwindow.location.href=/""+ getUri() +"/" + pageNumber;");
sb.append("/n}");
sb.append("/n/n");
sb.append("");
returnsb.toString();
}
publicintgetCurPage() {
returncurPage;
}
publicvoidsetCurPage(intcurPage) {
this.curPage = curPage;
}
publicintgetFirstPage() {
return1;
}
publicintgetLastPage() {
returnthis.getPageCount();
}
publicintgetNextPage()
{
if(curPage == getPageCount())
{
returncurPage;
}
returncurPage +1;
}
publicintgetPageCount()
{
if(resultCount==0)
{
this.curPage=0;
return0;
}
pageCount=(resultCount -1) / pageSize +1;
if(curPage>pageCount)
curPage=pageCount;
returnpageCount;
}
publicintgetPageSize() {
returnpageSize;
}
publicvoidsetPageSize(intpageSize) {
this.pageSize = pageSize;
}
publicintgetPrevPage()
{
if(1== curPage)
{
return1;
}
returncurPage -1;
}
publicintgetResultCount() {
returnresultCount;
}
publicvoidsetResultCount(intresultCount)
{
this.resultCount = resultCount;
}
publicString getUri() {
returnuri;
}
publicvoidsetUri(String uri)
{
intp=uri.lastIndexOf("curPage=");
if(p==-1)
{
uri=uri+"&curPage=";
}else
{
uri=uri.substring(0,p+8);
}
this.uri = uri;
}
//aa
publicList getData() {
returndata;
}
publicvoidsetData(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 "9";
}
StringBuffer sb = new StringBuffer();
sb.append("");
sb.append("9");
return sb.toString();
}
public String getCurPageTag()
{
if(0==getPageCount())
return "";
StringBuffer sb = new StringBuffer();
sb.append(" ");
sb.append("");
sb.append(this.getCurPage()+" ");
return sb.toString();
}
public String getLastPageTag()
{
if(0==getPageCount())
{
return ":";
}
StringBuffer sb = new StringBuffer();
sb.append("");
sb.append(":");
return sb.toString();
}
public String getPrevPageTag()
{
if(curPage==1||0==getPageCount())
{
return "7";
}
StringBuffer sb = new StringBuffer();
sb.append("");
sb.append("7");
return sb.toString();
}
public String getNextPageTag()
{
if(curPage==getPageCount()||0==getPageCount())
{
return "8";
}
StringBuffer sb = new StringBuffer();
sb.append("");
sb.append("8");
return sb.toString();
}
//commonTag
public String getcommonTag(int index)
{
return " "+index+"";
}
获得页码列表
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(""+resultCount+"记录 ");
sb.append("当前:第"+curPage+"/"+pageCount+"页 ");
sb.append("每页"+pageSize+"条记录 ");
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("");
int pageCount=this.getPageCount();
for(int n=1;n<=pageCount;n++)
{
if (n==curPage)
{
sb.append(""+n+"");
}else
{
sb.append(""+n+"");
}
}
sb.append("");
return sb.toString();
}
public String getSkipPage()
{
StringBuffer sb = new StringBuffer();
sb.append("
sb.append("/nfunction goPage(pageNumber) {");
sb.append("/nwindow.location.href=/"" + getUri() + "/" + pageNumber;");
sb.append("/n}");
sb.append("/n/n");
sb.append("");
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}效果如图: