今天闲来没事,写了一个页面的分页工具类,具有很好的兼容性与实用性哦。

不管怎么样,先来瞧一瞧:

package com.bw.utils;

/**
 * 分页工具类
 * 
 * @author H.ros
 *
 */
public class PageUtils2 {
	// 当前页(从页面获取的当前页码,未计算)
	private int currentPage;
	// 前一页
	private int prevPage;
	// 下一页
	private int nextPage;
	// 尾页
	private int lastPage;
	// 总记录数
	private int count;
	// 每页的条数
	private int pageSize = 3;
	// 分页计入数(使用时调用的初始页,计算后)
	private int pageRecord;
	// 页面分页模型(传入页面使用的DOM)
	private String page;

	// 有参构造器
	public PageUtils2(String currentPage, int count, int pageSize) {
		init(currentPage, count, pageSize);
		initLastPage();
		initCurrentPage();
		initPrevPage();
		initNextPage();
		initPageRecord();
		initPage();
	}

	// 初始化三个重要元素
	private void init(String currentPage, int count, int pageSize) {
		if (currentPage == null || currentPage == "" || currentPage == "0") {
			currentPage = "1";
		}
		this.currentPage = Integer.parseInt(currentPage);
		this.count = count;
		this.pageSize = pageSize;
	}

	// 初始化尾页
	private void initLastPage() {
		if (count % pageSize == 0) {
			lastPage = count / pageSize;
		} else {
			lastPage = count / pageSize + 1;
		}
	}

	// 初始化并矫正当前页(防止外部访问出错)
	private void initCurrentPage() {
		if (currentPage < 1) {
			currentPage = 1;
		} else if (currentPage > lastPage) {
			currentPage = lastPage;
		}
	}

	// 初始化上一页
	private void initPrevPage() {
		if (currentPage != 1) {
			prevPage = currentPage - 1;
		}else{
			prevPage = 1;
		}
	}

	// 初始化下一页
	private void initNextPage() {
		if (currentPage != lastPage) {
			nextPage = currentPage + 1;
		}else{
			nextPage = lastPage;
		}
	}

	// 初始化分页计入数
	private void initPageRecord() {
		pageRecord = (currentPage - 1) * pageSize;
        if(pageRecord < 0){
            pageRecord = 0;
        }
	}

	// 初始化页面分页模型(按键中的class属性是bootstrap的样式)
	private void initPage() {
		page = "第" + currentPage + "/" + lastPage + "页,共" + count + "条记录。";
		page += "<input type='button' value='首页' onclick='page(1)' class='btn btn-sm'/>";
		page += "<input type='button' value='上一页' onclick='page(" + prevPage + ")' class='btn btn-sm'/>";
		page += "<input type='button' value='下一页' onclick='page(" + nextPage + ")' class='btn btn-sm'/>";
		page += "<input type='button' value='尾页' onclick='page(" + lastPage + ")' class='btn btn-sm'/>";
	}

	/*
	 * 对外访问通道
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	public int getPrevPage() {
		return prevPage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public int getLastPage() {
		return lastPage;
	}

	public int getCount() {
		return count;
	}

	public int getPageSize() {
		return pageSize;
	}

	public int getPageRecord() {
		return pageRecord;
	}

	public String getPage() {
		return page;
	}

}

使用方法:

里面很多的地方都会计算好,使用时只用传入三个参数:

//currentPage是从前台传来的当前页码,count是数据的总统计数,pageSize是每页显示的条数,
//count必须是数据中的查询结果哦。这样才能与前台保持页码一致。
PageUtils util = new PageUtils(currentPage, count, pageSize);

 然后直接将它的一个Page属性传入前台页面就行:

mv.addObject("page", util.getPage());

在前台页面直接用EL表达式接收解析:

${page}

 在js中要设置一个函数:

function page(){}
//因为工具类中添加的是onclick的js点击事件,我们必须使用js的一个函数来进行后台处理;
//如果不想使用,在工具类中删除即可。

 

若有不对的地方,还望大神们不吝赐教。

更多推荐

手写分页工具类——Java