日期:2014-05-16 浏览次数:20422 次
首先:
1.工具类page:
package com.common.util;
import java.util.List;
@SuppressWarnings("unchecked")
public class Page
{
private int totalRows; // 总行数
private int pageSize; // 每页显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据库中的起始行
private List list;
public Page(int _totalRows, int _pageSize, int _currentPage)
{
?? totalRows = _totalRows;
?? pageSize = _pageSize;
?? currentPage = _currentPage;
?? // 计算总页数
?? totalPages = totalRows / pageSize;
?? int mod = totalRows % pageSize;
?? if (mod > 0)
?? {
totalPages++;
}
?? currentPage = 1;
?? startRow = 0;
}
public int getCurrentPage()
{
?? return currentPage;
}
public void setCurrentPage(int currentPage)
{
?? this.currentPage = currentPage;
}
/**
* 获得每页显示的行数
*
* @return
*/
public int getPageSize()
{
?? return pageSize;
}
public void setPageSize(int pageSize)
{
?? this.pageSize = pageSize;
}
/**
* 获得当前页在数据库中的起始行
*
* @return
*/
public int getStartRow()
{
?? return startRow;
}
public void setStartRow(int startRow)
{
?? this.startRow = startRow;
}
public int getTotalPages()
{
?? return totalPages;
}
public void setTotalPages(int totalPages)
{
?? this.totalPages = totalPages;
}
public int getTotalRows()
{
?? return totalRows;
}
public void setTotalRows(int totalRows)
{
?? this.totalRows = totalRows;
}
/**
* 第一页
*/
public void first()
{
?? currentPage = 1;
?? startRow = 0;
}
/**
* 前一页
*/
public void previous()
{
?? if (currentPage == 1)
?? {
??? return;
?? }
?? currentPage--;
?? startRow = (currentPage - 1) * pageSize;
}
/**
* 下一页
*/
public void next()
{
?? if (currentPage < totalPages)
?? {
??? currentPage++;
?? }
?? startRow = (currentPage - 1) * pageSize;
}
/**
* 最后一页
*/
public void last()
{
?? currentPage = totalPages;
?? startRow = (currentPage - 1) * pageSize;
}
/**
* 跳转到某一页
*/
public void any()
{
?? if (currentPage < 1)
?? {
??? currentPage = 1;
?? }
?? if (currentPage > totalPages)
?? {
??? currentPage = totalPages;
?? }
?? startRow = (currentPage - 1) * pageSize;
}
public void refresh(int _currentPage)
{
?? currentPage = _currentPage;
?? if (currentPage > totalPages)
?? {
??? last();
?? }
}
public List getList() {
?? return list;
}
public void setList(List list) {
?? this.list = list;
}
}
?
2.工具类pageHelp:
package com.common.util;
import javax.servlet.http.HttpServletRequest;
public class PageHelp
{
public static Page getPage(HttpServletRequest request, int totalRows,
??? int pageSize, int currentPage)
{
?? Page page = new Page(totalRows, pageSize, currentPage);
?? // 获得当前页号
?? String pagecount = currentPage + "";
??
?? // 如果当前页号为空,表示为首次查询该页
?? // 如果不为空,则刷新page对象,输入当前页号等信息
?? if (pagecount != null)
?? {
??? page.refresh(Integer.parseInt(pagecount));
?? }
??
?? // 获取当前执行的方法,首页,前一页,后一页,尾页。
?? String pagerMethod = "any";
?? if (request.getParameter("pageMethod") != null && !"undefined".equals(request.getParameter("pageMethod")))
?? {
??? pagerMethod = request.getParameter("pageMethod");
?? }
??
?? if (pagerMethod != null)
?? {
??? if (pagerMethod.equals("first"))
??? {
???? page.first();
??? }
??? else if (pagerMethod.equals("previous"))
??? {
???? page.previous();
??? }
??? else if (pagerMethod.equals("next"))
??? {
???? page.next();
??? }
??? else if (pagerMethod.equals("last