java分页代码



import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class PageUtil {
	private static int DEFAULT_PAGE_SIZE = 20; // 默认页面大小

	private int currentPage = 0;// 当前页
	private int rowCount = 0; // 总行数
	private int pageSize = 0; // 页大小
	private int pageCount = 0; // 总页数

	private int beginPosition = 0;// 页起始
	private int endPosition = 0;// 页终止

	private boolean hasNextPage = false;// 是否有下一页
	private boolean hasPreviousPage = false;// 是否上一页

	private ArrayList<Object> pagelist = new ArrayList<Object>(); // 记录集

	public PageUtil(ArrayList<Object> pagelist, String pageSize,
			String currentPage) {
		this.pagelist = pagelist;
		// 计算总行数
		this.rowCount = pagelist.size();

		// 页面大小不能小与等于0, 否则设置为默认页面大小
		this.pageSize = Integer.parseInt(pageSize);

		if (this.pageSize <= 0) {
			this.pageSize = DEFAULT_PAGE_SIZE;
		}
		// 计算出总页数
		this.getPageCount();

		// 当前页处理
		if (currentPage == null || currentPage.equals("")) {
			this.currentPage = 1;
		} else {
			try {
				this.currentPage = Integer.parseInt(currentPage);
			} catch (NumberFormatException nfe) {
				this.currentPage = 1;
			}
		}
		// 当前页范围处理
		this.getCurrentPage();
		// 页起始处理
		this.getBeginPosition();
		// 页结束处理
		this.getEndPosition();
	}

	public int getCurrentPage() {
		if (currentPage >= pageCount) {
			currentPage = pageCount;
		} else if (currentPage <= 1) {
			currentPage = 1;
		}
		return currentPage;
	}

	public int getBeginPosition() {
		// 计算出页起始
		beginPosition = (currentPage - 1) * pageSize + 1;
		return beginPosition;
	}

	public int getEndPosition() {
		// 计算出页终止
		if (currentPage >= pageCount) {
			endPosition = rowCount;
		} else {
			endPosition = currentPage * pageSize;
		}
		return endPosition;
	}

	public int getRowCount() {
		return rowCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageCount() {
		// 计算出总页数
		pageCount = (rowCount + pageSize - 1) / pageSize;
		return pageCount;
	}

	public boolean isHasNextPage() {
		// 计算出是否有下一页
		if (currentPage >= pageCount) {
			hasNextPage = false;
		} else {
			hasNextPage = true;
		}
		return hasNextPage;
	}

	public boolean isHasPreviousPage() {
		// 计算出是否有上一页
		if (currentPage <= 1) {
			hasPreviousPage = false;
		} else {
			hasPreviousPage = true;
		}
		return hasPreviousPage;
	}

	public int getPreviousPage() {
		if (this.isHasPreviousPage()) {
			return currentPage - 1;
		}
		return currentPage;
	}

	public int getNextPage() {
		if (this.isHasNextPage()) {
			return currentPage + 1;
		} else {
			return currentPage;
		}
	}

	public ArrayList<Object> getPageList() {
		ArrayList<Object> list = new ArrayList<Object>();
		
		if(pagelist != null && pagelist.size() > 0)
		{
			for (int i = beginPosition; i <= endPosition; i++) {
				list.add(pagelist.get(i - 1));
			}
		}

		return list;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList<Object> pagelist = new ArrayList<Object>();
		
		for(int i = 0; i < 2001 ; i++)
		{
			pagelist.add((Object) new HashMap<String, Object>().put("key", i));
		}

		String pageSize = "20";
		String currentPage = "6";
		PageUtil pu = new PageUtil(pagelist, pageSize, currentPage);
		
		PageHtmlUtil phu = new PageHtmlUtilImpl();

		String pagehtml = phu.getPageHtml("/sysmanage/sysmenu", pu);
		System.out.println("rowcount:"+pu.getRowCount());
		System.out.println("pagecount:"+pu.getPageCount());
		System.out.println(pagehtml);
	}
}

PageHtmlUtilImpl.java

public class PageHtmlUtilImpl implements PageHtmlUtil{

	@Override
	public String getPageHtml(String actionurl, PageUtil pageutil) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer();
		/** 分页头    开始  */
		sb.append("<div class=\"pagination\">");
		sb.append("<span class=\"page_no\">Page ");
		sb.append(pageutil.getCurrentPage());
		sb.append(" of ");
		sb.append(pageutil.getPageCount());
		sb.append("</span>");
		sb.append("<ul class=\"pag_list\">");
		sb.append("<li><a href=\"");
		if(pageutil.isHasPreviousPage())
		{
			sb.append(actionurl);
			sb.append(pageutil.getPreviousPage());
		}
		else
		{
			sb.append("javascript:void(0)");
		}
		sb.append("\" class=\"button light_blue_btn\"><span><span>PREVIOUS</span></span></a></li>");
		/** 分页头    结束 */
		

		if( pageutil.getCurrentPage() - 6 >= 0 )
		{
			
			sb.append(getClassPageRow(actionurl,pageutil.getCurrentPage(),1));
			sb.append(getClassPageRow(actionurl,pageutil.getCurrentPage(),2));
			sb.append("<li>[...]</li>");
			
			int s_num = pageutil.getCurrentPage() - 2;
			int e_num = 0;
			boolean nonext = false;
			if((pageutil.getCurrentPage() + 4) - pageutil.getPageCount() >= 0)
			{
				e_num = pageutil.getPageCount();
			}
			else
			{
				e_num = pageutil.getCurrentPage() + 2;
				nonext = true;
			}
			
			for( int i = s_num; i <= e_num; i++ )
			{
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), i));
			}

			System.out.println(pageutil.getPageCount() - (pageutil.getCurrentPage() + 5));
			
			if( nonext )
			{
				sb.append("<li>[...]</li>");
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount() - 1));
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount()));
			}
		}
		else
		{
			int s_num = pageutil.getCurrentPage() - 5 <= 0?1:pageutil.getCurrentPage() - 5;
			int e_num = 0;
			
			if(s_num - 2 <= 0)
			{
				e_num = pageutil.getPageCount() - pageutil.getCurrentPage() <=2?pageutil.getPageCount():pageutil.getCurrentPage() + 2;
			}
			else
			{
				e_num = s_num -(s_num - 5);
			}
			
			for( int i = s_num; i <= e_num; i++ )
			{
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), i));
			}
			
			if(pageutil.getPageCount() - e_num == 2)
			{
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount() - 1));
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount()));
			}
			else if(pageutil.getPageCount() - e_num == 1)
			{
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount()));
			}
			else if(pageutil.getPageCount() - e_num > 2)
			{
				sb.append("<li>[...]</li>");
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount() - 1));
				sb.append(getClassPageRow(actionurl, pageutil.getCurrentPage(), pageutil.getPageCount()));
			}
		}
		
		/** 分页未    开始  */
        sb.append("<li><a href=\"");
		if(pageutil.isHasNextPage())
		{
			sb.append(actionurl);
			sb.append(pageutil.getNextPage());
		}
		else
		{
			sb.append("javascript:void(0)");
		}
		sb.append("\" class=\"button light_blue_btn\"><span><span>NEXT</span></span></a> </li>");
		sb.append("</ul>");
		sb.append("</div>");
		/** 分页未    开始  */
		
		return sb.toString();
	}
	
	/**
	 * 
	 * @param actionurl	action的url
	 * @param curpage	当前页
	 * @param pagenum	这个页面号
	 * @return
	 */
	public static String getClassPageRow( String actionurl, int curpage, int pagenum )
	{
		StringBuffer sb = new StringBuffer();
		// 当前页class处理
		if(curpage == pagenum)
		{
			sb.append("<li><a href=\"");
			sb.append(actionurl);
			sb.append(pagenum);
			sb.append("\" class=\"current_page\"><span><span>");
			sb.append(pagenum);
			sb.append("</span></span></a></li>");
		}
		else
		{
			sb.append("<li><a href=\"");
			sb.append(actionurl);
			sb.append(pagenum);
			sb.append("\" >");
			sb.append(pagenum);
			sb.append("</a></li>");
		}
		return sb.toString();
	}
	
	public String getLastPage()
	{
		return "";
	}
}


你可能感兴趣的:(java,分页)