问题描述
使用分页类,直接调用就可以,代码如下:packagecom.godwin.news.util;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.util.ArrayList;/***分页类,用于封闭分页显示信息**@authorjavabs**/publicclassPager{//当前页privateintcurrentPage;//总页数privateinttotalPage;//总记录数privateinttotalRecord;//每页条数privateintpageSize;//是否有下一页privateinthasNext;privateArrayListkeys;privateArrayListvalues;//查询串privateStringqueryString;//首页privateStringfirstLink;//上一页privateStringpreviousLink;//下一页privateStringnextLink;//尾页privateStringlastLink;//链接privateStringforScriptLink;publicPager(intpageSize,StringqueryString){keys=newArrayList();values=newArrayList();setQueryString(queryString);setPageSize(pageSize);}publicStringgetForScriptLink(){if(keys.contains("toPage"))removeKey("toPage");Stringtmp=getQueryString();if(tmp.length()==0)return"?";elsereturn"?"+tmp+"&";}publicintgetCurrentPage(){returncurrentPage;}publicvoidsetCurrentPage(StringtoPage){inttmpage=1;try{tmpage=Integer.parseInt(toPage);}catch(NumberFormatExceptione){tmpage=1;}if(tmpage<1)tmpage=1;elseif(tmpage>getTotalPage())tmpage=getTotalPage();currentPage=tmpage;}publicStringgetFirstLink(){returngetQueryStr(1);}publicintgetHasNext(){inti=1;if(getCurrentPage()>=getTotalPage())i=0;returni;}publicStringgetLastLink(){returngetQueryStr(getTotalPage());}publicStringgetNextLink(){returngetQueryStr(currentPage==totalPage?currentPage:(currentPage+1));}publicintgetPageSize(){returnpageSize;}publicvoidsetPageSize(intpageSize){this.pageSize=pageSize;}publicStringgetPreviousLink(){returngetQueryStr(currentPage==1?currentPage:currentPage-1);}publicvoidsetPreviousLink(StringpreviousLink){this.previousLink=previousLink;}publicStringgetQueryString(){StringBuffersb=newStringBuffer();for(inti=0;i<keys.size();i++){Stringkey=(String)keys.get(i);Stringvalue=(String)values.get(i);sb.append("&");sb.append(key);sb.append("=");sb.append(value);}returnsb.delete(0,1).toString();}publicvoidsetQueryString(StringqueryString){if(queryString!=null){Strings[]=queryString.split("&");for(inti=0;i<s.length;i++){Strings1[]=s[i].split("=");if(s1.length==2){keys.add(s1[0]);values.add(s1[1]);}else{keys.add(s1[0]);values.add("");}}}}publicintgetTotalPage(){returntotalPage;}publicvoidsetTotalPage(){if(totalRecord%pageSize==0){totalPage=totalRecord/pageSize;totalPage=totalPage==0?1:totalPage;}elsetotalPage=totalRecord/pageSize+1;}publicintgetTotalRecord(){returntotalRecord;}publicvoidsetTotalRecord(inttotalRecord){this.totalRecord=totalRecord;setTotalPage();}publicString[]getQueryParameterValues(Stringkey){returngetQueryParameterValues(key,"UTF-8");}publicString[]getQueryParameterValues(Stringkey,Stringdecode){ArrayListret=newArrayList();for(inti=0;i<keys.size();i++)if(((String)keys.get(i)).equals(key))try{ret.add(URLDecoder.decode((String)values.get(i),decode));}catch(UnsupportedEncodingExceptione){ret.add((String)values.get(i));}if(ret.size()==0)returnnull;StringstrArr[]=newString[ret.size()];for(inti=0;i<ret.size();i++)strArr[i]=(String)ret.get(i);returnstrArr;}publicStringgetQueryParameter(Stringkey){returngetQueryParameter(key,"UTF-8");}publicStringgetQueryParameter(Stringkey,Stringdecode){Stringvalue="";if(key!="toPage"){try{value=URLDecoder.decode(getValue(key),decode);}catch(UnsupportedEncodingExceptione){value=getValue(key);}}else{inttmpage=1;try{value=getValue(key);tmpage=Integer.parseInt(value);}catch(NumberFormatExceptione){tmpage=1;}catch(NullPointerExceptione1){tmpage=1;}if(tmpage<1)tmpage=1;elseif(tmpage>getTotalPage())tmpage=getTotalPage();value=(newStringBuffer(String.valueOf(tmpage))).toString();}returnvalue;}publicvoidsetQueryParameter(Stringkey,Stringvalue){if(key.equals("toPage"))removeKey(key);keys.add(key);values.add(value);}publicStringgetQueryStr(inttoPage){setQueryParameter("toPage",(newStringBuffer(String.valueOf(toPage))).toString());return"?"+getQueryString();}privateStringgetValue(Stringkey){Stringret="";for(inti=0;i<keys.size();i++){if(!((String)keys.get(i)).equals(key))continue;ret=(String)values.get(i);break;}returnret;}privatevoidremoveKey(Stringkey){for(inti=0;i<keys.size();i++){if(!((String)keys.get(i)).equals(key))continue;keys.remove(i);values.remove(i);break;}}publicstaticvoidmain(Stringargs[]){Stringstr="a=中文aa&b=2&c=3&c=4&c=5";Pagerpage=newPager(2,str);page.setTotalRecord(10);page.setCurrentPage("1");System.out.println(page.getFirstLink());System.out.println(page.getPreviousLink());System.out.println(page.getNextLink());System.out.println(page.getLastLink());Strings=page.getQueryParameter("a");System.out.println("s:"+s);}}
解决方案
解决方案二:
什么情况?
解决方案三:
如果是备忘,请发非技术相关.不需要分.
解决方案四:
引用2楼luozhangwen的回复:
如果是备忘,请发非技术相关.不需要分.
+1
解决方案五:
这是个不错的类,收藏了!谢谢楼主分享!