`

Java 翻页工具类

    博客分类:
  • Java
阅读更多

@RequestMapping(value = "list.action")
public String list(HttpServletRequest request, HttpServletResponse response) {
	int _pageSize = 20;//每页显示数量
	int _totalRecord = cmsArticleSer.countText();//总记录数
	int _currentPage = 1;//当前页码
	PageUtil page = new PageUtil();
	// 当前页码
	if (request.getParameter("pageNo") != null && !request.getParameter("pageNo").equals("")) {
		_currentPage = Integer.parseInt(request.getParameter("pageNo"));
	}
	page.setPageSize(_pageSize);// 每页显示的记录数量
	page.setTotalRecord(_totalRecord);// 总记录数
	page.setCurrentPage(_currentPage);// 当前页码
	cu.setStart(page.getStart());//开始记录数
	cu.setLimit(page.getPageSize());//一次提取多少条记录
	List<Test> list = getTestList(cu);
	request.setAttribute("list", list);
	this.setPage(page, request);
	return "cms/list";
}

public void setPage(PageUtil page, HttpServletRequest request) {
	// 每页显示的记录数量
	request.setAttribute("pageSize", page.getPageSize());
	// 总记录数
	request.setAttribute("totalRecord", page.getTotalRecord());
	// 当前页码
	request.setAttribute("currentPage", page.getCurrentPage());
	// 总页数
	request.setAttribute("totalPage", page.getTotalPage());
	// 当前页开始记录
	request.setAttribute("currentStartRecord", page.getCurrentStartRecord());
	// 当前页结束记录
	request.setAttribute("currentEndRecord", page.getCurrentEndRecord());
	// 向上翻页页码
	request.setAttribute("upPage", page.getUpPage());
	// 向下翻页页码
	request.setAttribute("downPage", page.getDownPage());
}

//数据库操作
@SuppressWarnings("unchecked")
public List<Test> getTestList(TestUtil cu) {
	List<Test> list = new ArrayList<CmsArticle>();
	StringBuffer sb = new StringBuffer();
	try {
		sb.append("from Test ");
		Query query = this.getSession().createQuery(sb.toString());
		query.setFirstResult(cu.getStart());
		query.setMaxResults(cu.getLimit());
		list = query.list();
	} catch (Exception e) {
		System.out.println(e);
	}
	return list;
}


/**
 * 翻页类 1.首先设置 每页显示的记录数量 2.再次设置 总记录数 3.最后设置 当前页码
 * 
 * @author Administrator
 */
public class PageUtil {

	private int pageSize;// 每页显示的记录数量
	private int totalRecord;// 总记录数
	private int currentPage;// 当前页码
	// ***********************************************
	private int totalPage;// 总页数
	private int currentStartRecord;// 当前页开始记录
	private int currentEndRecord; // 当前页结束记录
	private int start;// 开始记录
	private int end;// 结束记录
	private int upPage;// 向上翻页页码
	private int downPage;// 向下翻页页码

	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置每页显示的记录数量
	 * 
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalRecord() {
		return totalRecord;
	}

	/**
	 * 设置总记录数
	 */
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
		// 设置总页数
		int _totalPage = 1;
		if (getTotalRecord() % getPageSize() == 0) {
			// 如果总记录数除以每页显示条数可以整除,商就是总页码
			_totalPage = this.getTotalRecord() / this.getPageSize();
		} else {
			// 如果总记录数除以每页显示条数不能整除,商加1才是总页码
			_totalPage = this.getTotalRecord() / this.getPageSize() + 1;
		}
		if (_totalPage < 1) {
			_totalPage = 1;
		}
		this.setTotalPage(_totalPage);
	}

	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 设置页码
	 */
	public void setCurrentPage(int currentPage) {
		if (currentPage > this.getTotalPage()) {
			this.currentPage = this.getTotalPage();
		} else {
			this.currentPage = currentPage;
		}
		// 设置当前页开始记录
		int _currentStartRecord = 0;
		if (this.getTotalRecord() != 0) {
			_currentStartRecord = (this.getCurrentPage() - 1)
					* this.getPageSize() + 1;
		}
		if (_currentStartRecord < 0) {
			_currentStartRecord = 0;
		}
		this.setCurrentStartRecord(_currentStartRecord);
		// 设置当前页结束记录
		int _currentEndRecord = this.getCurrentPage() * this.getPageSize();
		if (_currentEndRecord > this.getTotalRecord()) {
			_currentEndRecord = this.getTotalRecord();
		}
		this.setCurrentEndRecord(_currentEndRecord);
		// 开始记录
		int _start = (this.getCurrentPage() - 1) * this.getPageSize();
		if (_start < 0) {
			_start = 0;
		}
		this.setStart(_start);
		// 结束记录
		int _end = this.getCurrentPage() * this.getPageSize();
		if (_end > this.getTotalRecord()) {
			_end = this.getTotalRecord();
		}
		this.setEnd(_end);
		// 向上翻页页码
		int _upPage = this.getCurrentPage() - 1;
		if (_upPage < 1) {
			_upPage = 1;
		}
		this.setUpPage(_upPage);
		// 向下翻页页码
		int _downPage = this.getCurrentPage() + 1;
		if (_downPage > this.getTotalPage()) {
			_downPage = this.getTotalPage();
		}
		this.setDownPage(_downPage);
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getCurrentStartRecord() {
		return currentStartRecord;
	}

	public void setCurrentStartRecord(int currentStartRecord) {
		this.currentStartRecord = currentStartRecord;
	}

	public int getCurrentEndRecord() {
		return currentEndRecord;
	}

	public void setCurrentEndRecord(int currentEndRecord) {
		this.currentEndRecord = currentEndRecord;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getUpPage() {
		return upPage;
	}

	public void setUpPage(int upPage) {
		this.upPage = upPage;
	}

	public int getDownPage() {
		return downPage;
	}

	public void setDownPage(int downPage) {
		this.downPage = downPage;
	}

}


jsp页面
<table width="93%" height="39" border="0" align="center"cellpadding="0" cellspacing="0" class="title_bg">
	<tr>
		<td>
			<form id="qbmh_list_form" name="qbmh_list_form" method="post" action="list2.action">
				<c:if test="${currentPage == '1'}">
					首页
				</c:if>
				<c:if test="${currentPage != '1'}">
					<a href="javascript: gotoPage('1')">首页</a>
					<a href="javascript: gotoPage('${upPage}')">上一页</a> 
				</c:if>
				第<input name="pageNo" id="pageNo" type="text" class="testpage" value="${currentPage}" />页
				共${totalRecord}条记录
				每页${pageSize}条记录
				共${totalPage}页
				第${currentStartRecord}条—第${currentEndRecord}条 记录
				<c:if test="${currentPage != totalPage}">
					<a href="javascript: gotoPage('${downPage}')">下一页<a href="javascript: gotoPage('${totalPage}')">尾页</a>
				</c:if>
				<c:if test="${currentPage == totalPage}">
					尾页
				</c:if>
			</form>
		</td>
	</tr>
</table>


<script type="text/javascript" language="javascript">
//键盘捕捉
function document.onkeydown(){
	//回车
	if(event.keyCode == '13'){
		var _pageNo = document.getElementById("pageNo").value;
		gotoPage(_pageNo);
	}
}
//翻页
function gotoPage(pageNo){
	var re = /^[1-9]\d*$/;
	if (!re.test(pageNo)) {
		pageNo="1";
	}
	document.getElementById("pageNo").value=pageNo;
	document.getElementById("qbmh_list_form").submit();
}
</script>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics