ページングアルゴリズム



/**
 * <p>Title:      </p>
 */
public class Pages {
	
	private int arttotal;
	private int pagetotal;
	private int curPage;
	private int pagesize;
	private String pageInfo;
	
	/**
	 * @return       
	 */
	public String getPageInfo() {
		return pageInfo;
	}
	
	/**
	 * @return       
	 */
	public int getArttotal() {
		return arttotal;
	}
	/**
	 * @return       
	 */
	public int getCurPage() {
		return curPage;
	}

	/**
	 * @return     
	 */
	public int getPagetotal() {
		return pagetotal;
	}

	/**
	 * @param arttotal           
	 * @param curPage     
	 * @param pagesize     
	 * @param link         
	 */
	public Pages(int arttotal,int curPage,int pagesize,String link) {
			this.arttotal = arttotal;
			this.curPage = curPage;
			this.pagesize = pagesize;
			doPages();
			pageInfo(link);

	}
	
	/**
	 *           
	 */
	private void doPages(){
		pagetotal = arttotal%pagesize==0?arttotal/pagesize:arttotal/pagesize+1;
		if(pagetotal==0){
			pagetotal=1;
		}
		if(curPage<1){
			curPage=1;
		}else if(curPage>pagetotal){
				curPage=pagetotal;
		}
	}
	
	/**        ,       html         。
	 * @param link              
	 */
	private void pageInfo(String link){
		StringBuffer sb = new StringBuffer();
		if(pagetotal>1){
		sb.append("<table class='tab' width='100%' height='100%' cellSpacing=0 cellPadding=0>");
		sb.append("<tr><td>&nbsp;&nbsp; <b>");
		sb.append(pagetotal);
		sb.append("</b> &nbsp;&nbsp;<b>");
		sb.append(arttotal);
		sb.append("</b>   &nbsp;&nbsp;   <b>");
		sb.append(curPage);
		sb.append("</b> &nbsp;&nbsp;&nbsp;<a href=");
		sb.append(link);
		sb.append("&curPage=1>");
		sb.append("<img src='webapp/images/first.gif' border=0 alt=  ></a>");
		sb.append("<a href=");
		sb.append(link);
		sb.append("&curPage=");
		sb.append(curPage>=2?curPage-1:curPage);
		sb.append("><img src='webapp/images/previous.gif' border=0 alt=   ></a>&nbsp;&nbsp;[");
		int pagebegin = 1;
		int pageend = pagetotal;
		if(curPage <= 5 && pagetotal - curPage > 5){
			pageend = pagetotal < 10 ? pagetotal : 10;
		}else
	        if(curPage > 5 && pagetotal - curPage > 5) {
		        	pagebegin = curPage - 4;
		        	pageend = curPage + 5;
		        } else{
			        if(curPage > 5 && pagetotal - curPage <= 5)
			        	pagebegin = pagetotal < 10 ? 1 : pagetotal - 9;
		        }
		 for(int i = pagebegin; i <= pageend; i++){
             if(i == curPage){
                 sb.append("<b>");
                 sb.append(i);
                 sb.append("</b> ");
             } else{
            	 sb.append(" <a href='");
                 sb.append(link);
                 sb.append("&curPage=");
                 sb.append(i);
                 sb.append("'>");
                 sb.append(i);
                 sb.append("</a> ");
              }
		 }
		sb.append("]&nbsp;&nbsp;<a href=");
		sb.append(link);
		sb.append("&curPage=");
		sb.append(curPage<=pagetotal-1?curPage+1:pagetotal);
		sb.append(">");
		sb.append("<img src='webapp/images/next.gif' border=0 alt=   ></a>");
		sb.append("<a href=");
		sb.append(link);
		sb.append("&curPage=");
		sb.append(pagetotal);
		sb.append("><img src='webapp/images/last.gif' border=0 alt=  ></a>");
		sb.append("</td></tr>");
		sb.append("</table>");
		}else{
			sb.append("&nbsp;&nbsp; <b>");
			sb.append(pagetotal);
			sb.append("</b> &nbsp;&nbsp;<b>");
			sb.append(arttotal);
			sb.append("</b>   &nbsp;&nbsp;");
		}
		pageInfo = sb.toString();
		sb = null;
	}
}