⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractviewpage.java

📁 java分页bean
💻 JAVA
字号:
package com.page;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页默认抽象实现 初始时,分页类有下列默认值: 分页大小为-1,为不分页; 总页数为1页 当前页为第一页 总记录数为0条
 * 当前页数据列表为没有任何记录的列表
 * 
 * @author dl
 * 
 */
public abstract class AbstractViewPage implements ViewPage {

	// -----------------------------------------
	// 私有静态常量
	// -----------------------------------------

	private static final int DEFAULT_PAGE_INDEX = 1;

	private static final int DEFALT_PAGE_COUNT = 1;

	private static final int DEFAULT_PAGE_SIZE = -1;

	private static final int DEFAULT_ROWS = 0;

	// -----------------------------------------
	// 私有成员变量
	// -----------------------------------------

	/** 当前页索引号* */
	private int pageIndex = DEFAULT_PAGE_INDEX;

	/** 总页数* */
	private int pageCount = DEFALT_PAGE_COUNT;

	/** 分页大小* */
	private int pageSize = DEFAULT_PAGE_SIZE;

	/** 数据总记录数* */
	private int rows = DEFAULT_ROWS;

	// ------------------------------------------
	// 本地成员变量getter,setter方法
	// ------------------------------------------

	/**
	 * 设置新页号,只有大于等于1而且小于等于总页数并且不为当前页时,才允许设置
	 * 
	 * @param pageIndex
	 *            The pageIndex to set.
	 */
	private void setPageIndex(int newPageIndex) {

		if (newPageIndex >= this.DEFAULT_PAGE_INDEX
				&& newPageIndex <= this.getPageCount()
				&& newPageIndex != this.pageIndex) {
			this.pageIndex = newPageIndex;
		}

	}

	/**
	 * @return Returns the rows.
	 */
	private int getRows() {
		return rows;
	}

	/**
	 * @param rows
	 *            The rows to set.
	 */
	private void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * @param pageCount
	 *            The pageCount to set.
	 */
	private void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	// --------------------------------------
	// 实现Page接口方法
	// --------------------------------------

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageData()
	 */
	public List getPageData() {
		List pageList = null;

		// 获得当前页数据
		pageList = this.pageList(this.getPageFirstRecord(), this.getPageRows());
		// 保证不返回null
		if (pageList == null) {
			pageList = new ArrayList();
		}
		return pageList;

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageIndex()
	 */
	public int getPageIndex() {
		return this.pageIndex;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#isFirstPage()
	 */
	public boolean isFirstPage() {

		return this.DEFAULT_PAGE_INDEX == this.pageIndex;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#isLastPage()
	 */
	public boolean isLastPage() {
		// 当前页索引为总页数时为最后一页
		return this.pageIndex == this.pageCount;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getHashNextPage()
	 */
	public boolean getHashNextPage() {
		// 当前页索引号小于总页数
		return this.pageIndex < this.pageCount;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getHashPreviousPage()
	 */
	public boolean getHashPreviousPage() {
		// 当前页索引号大于默认的初始页号,这里为1
		return this.pageIndex > this.DEFAULT_PAGE_INDEX;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageCount()
	 */
	public int getPageCount() {

		return this.pageCount;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageSize()
	 */
	public int getPageSize() {

		return this.pageSize;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageRows()
	 */
	public int getPageRows() {
		// 当页面大小为-1 时,返回总记录数
		if (this.DEFAULT_PAGE_SIZE == this.pageSize) {
			return this.rows;
		}

		// 不为最后一页时,返回pageSize
		if (!this.isLastPage()) {
			return this.pageSize;
		}
		// 最后一页时
		return this.rows - (this.pageSize * (this.pageCount - 1));
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageFirstRecord()
	 */
	public int getPageFirstRecord() {

		// 页大小为-1 时
		if (this.DEFAULT_PAGE_SIZE == this.pageSize) {
			return 0;
		}
		return (this.pageIndex - 1) * this.pageSize;
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#getPageLastRecord()
	 */
	public int getPageLastRecord() {
		// 页大小为-1时,返回总记录数
		if (this.DEFAULT_PAGE_SIZE == this.pageSize) {
			return this.rows;
		}
		return this.getPageFirstRecord() + this.getPageRows();
	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#gotoFirstPage()
	 */
	public void gotoFirstPage() {
		this.gotoPage(this.DEFAULT_PAGE_INDEX);

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#gotoLastPage()
	 */
	public void gotoLastPage() {
		this.gotoPage(this.getPageCount());

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#gotoPreviousPage()
	 */
	public void gotoPreviousPage() {
		this.gotoPage(this.getPageIndex() - 1);

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#gotoNextPage()
	 */
	public void gotoNextPage() {
		this.gotoPage(this.getPageIndex() + 1);

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#gotoPage(int)
	 */
	public void gotoPage(int newPageIndex) {
		if (newPageIndex >= this.DEFAULT_PAGE_INDEX
				&& newPageIndex <= this.getPageCount()) {
			this.setPageIndex(newPageIndex);
		}

	}

	/**
	 * @see com.palic.elis.pos.junit.counter.web.mvc.ViewPage#setPageSize(int)
	 */
	public void setPageSize(int size) {
		if (size < 1) {
			size = 1;
		}

		this.pageSize = size;

		// 进行初始化
		this.doInit();
	}

	// -----------------------------------
	// 辅助方法
	// -----------------------------------

	/**
	 * 分页初始化方法,为了保证总是能正确的初始化,所以声明为final ,为了让子类可以调用声明为protected
	 * 
	 */
	protected final void doInit() {
		int rows = 0;

		// 获得总记录数
		rows = totalRows();

		// 设置总记录数
		this.setRows(rows);
		// 设置新的总页数

		// 计算并设置总页数
		int pages = calculatePageCount();
		this.setPageCount(pages);

		// 转到第一页
		this.gotoPage(this.DEFAULT_PAGE_INDEX);

		onInit();
	}

	/**
	 * 记算总页数
	 * 
	 * @return 总页数
	 */
	private int calculatePageCount() {

		// 总记录数为0条,则返回的总页数为1
		if (this.getRows() == 0) {
			return this.DEFALT_PAGE_COUNT;
		}

		// 如果页面大小为-1,则返回的总页数为1
		if (this.DEFAULT_PAGE_SIZE == this.getPageSize()) {
			return this.DEFALT_PAGE_COUNT;
		}

		return this.getRows() / this.getPageSize()
				+ (this.getRows() % this.getPageSize() == 0 ? 0 : 1);
	}

	/**
	 * 获得总记录数,调用queryTotalRows(),将异常封装为non-checked 异常
	 * 
	 * @return 总记录数
	 * @throws ApplicationRuntimeException
	 */
	private int totalRows() {
		return queryTotalRows();
	}

	/**
	 * 获得当前页数据,调用queryPageList()方法,将异常封装为non-checked异常
	 * 
	 * @param startRow
	 *            开始记录号
	 * @param rowCount
	 *            记录总数
	 * @return 当前页数据
	 * @throws ApplicationRuntimeException
	 */
	private List pageList(int startRow, int rowCount) {
		return queryPageList(startRow, rowCount);
	}

	// -----------------------------------------
	// 子类实现的方法
	// -----------------------------------------

	/**
	 * 初始化附加方法,由子类扩展
	 */
	protected void onInit() {

	}

	/**
	 * 查询获得总记录数,由子类具体实现
	 * 
	 * @return 总记录数
	 * @throws Exception
	 */
	protected abstract int queryTotalRows();

	/**
	 * 查询当前页数据,从startRow 开始的rowCount条记录
	 * 
	 * @param startRow
	 *            开始记录号
	 * @param rowCount
	 *            记录总数
	 * @return 当前页数据
	 * @throws Exception
	 */
	protected abstract List queryPageList(int startRow, int rowCount);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -