/**
* 封装分页逻辑
* @author Administrator
*
*/
public class PageModel<T> {
//结果集
private List<T> list;
//记录数
private int totalRecords;
//每页多少条数据
private int pageSize;
//第几页
private int pageNo;
/**
* 返回总页数
* @return
*/
public int getTotalPages() {
return (totalRecords + pageSize - 1) / pageSize;
}
/**
* 首页
* @return
*/
public int getTopPageNo() {
return 1;
}
/**
* 上一页
* @return
*/
public int getPreviousPageNo() {
if (this.pageNo <= 1) {
return 1;
}
return this.pageNo - 1;
}
/**
* 下一页
* @return
*/
public int getNextPageNo() {
if (this.pageNo >= getButtomPageNo()) {
return getButtomPageNo();
}
return this.pageNo + 1;
}
/**
* 尾页
* @return
*/
public int getButtomPageNo() {
return getTotalPages();
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageNo() {
return pageNo;
}
public void setPageNo(int pageNo) {
this.pageNo = pageNo;
}
}
另一种写法:
public class PageUtils {
/**
* 默认第一页页码
*/
public static final int INDEX_PAGE_NUM = 1;
public final static int PAGE_SIZE = 15; // 默认分页大小
/**
* 计算页数
*/
public static int pageCount(int recordCount, int perPage) {
int pc = (int) Math.ceil(recordCount / (double) perPage);
if (pc == 0)
pc = 1;
return pc;
}
/**
* 计算查询时,数据当前位置(fromIndex)
*/
public static int fromIdx(int page, int fetchCount) {
return (page - INDEX_PAGE_NUM) * fetchCount;
}
/**
* 分页条
*
* @param currentPage
* 当前页数
* @param fetchCount
* 每页显示数
* @param recordCount
* 数据总数
* @param pagePath
* 页面路径
* @return
*/
public static String getPageStr(int currentPage, int fetchCount,
int recordCount, String fileName,String sufFix) {
// String fileName = pagePath.split("\\.")[0];
// String sufFix = "." + pagePath.split("\\.")[1];
StringBuffer result = new StringBuffer();
String blankStr = " ";
int maxLength = 4;
int pageCount = pageCount(recordCount, fetchCount);
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
result.append("<a href='" + fileName + sufFix + "'>首页</a>");
if (currentPage > INDEX_PAGE_NUM) {
result.append(blankStr);
result.append("<a href='" + fileName);
if (currentPage - 1 != INDEX_PAGE_NUM) {
result.append("_" + (currentPage - 1));
}
result.append(sufFix + "'>上一页</a>");
}
result.append("</span>");
result.append(blankStr);
}
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
for (int cou = ((currentPage - maxLength) > 0 ? maxLength
: currentPage - 1); cou >= 1; cou--) {
result.append("<a href='" + fileName);
if (currentPage - cou != INDEX_PAGE_NUM) {
result.append("_" + (currentPage - cou));
}
result.append(sufFix + "'>" + (currentPage - cou) + "</a>");
result.append(blankStr);
}
result.append("</span>");
result.append("<span class='pageon'>" + currentPage + "</span>");
result.append("<span class='pagelink'>");
for (int cou = 1; cou <= ((currentPage + maxLength) > pageCount ? pageCount
- currentPage
: maxLength); cou++) {
result.append("<a href='" + fileName + "_"
+ (currentPage + cou) + sufFix + "'>"
+ (currentPage + cou) + "</a>");
result.append(blankStr);
}
result.append("</span>");
}
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
if (currentPage < pageCount) {
result.append("<a href='" + fileName + "_" + (currentPage + 1)
+ sufFix + "'>下一页</a>");
}
result.append(blankStr);
result.append("<a href='" + fileName + "_" + pageCount + sufFix
+ "'>末页</a>");
result.append("</span>");
}
return result.toString();
}
/**
* 动态分页条(自动带"page=?"的参数)
*
* @param currentPage
* 当前页数
* @param fetchCount
* 每页显示数
* @param recordCount
* 数据总数
* @param pagePath
* 页面路径
* @param reqParam
* 页面参数
* @return
*/
public static String getParamPageStr(int currentPage, int fetchCount,
int recordCount, String pagePath, String reqParam) {
String fileName = pagePath.split("\\.")[0];
String sufFix = "." + pagePath.split("\\.")[1];
StringBuffer result = new StringBuffer();
String blankStr = " ";
int maxLength = 4;
int pageCount = pageCount(recordCount, fetchCount);
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
result.append("<a href='" + fileName + sufFix + "?" + reqParam
+ "&page=1" + "'>首页</a>");
if (currentPage > INDEX_PAGE_NUM) {
result.append(blankStr);
result.append("<a href='" + fileName);
result.append(sufFix + "?" + reqParam + "&page=");
result.append(currentPage - 1);
result.append("'>上一页</a>");
}
result.append("</span>");
result.append(blankStr);
}
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
for (int cou = ((currentPage - maxLength) > 0 ? maxLength
: currentPage - 1); cou >= 1; cou--) {
result.append("<a href='" + fileName);
result.append(sufFix + "?" + reqParam + "&page=");
result.append(currentPage - cou);
result.append("'>" + (currentPage - cou) + "</a>");
result.append(blankStr);
}
result.append("</span>");
result.append("<span class='pageon'>" + currentPage + "</span>");
result.append("<span class='pagelink'>");
for (int cou = 1; cou <= ((currentPage + maxLength) > pageCount ? pageCount
- currentPage
: maxLength); cou++) {
result.append("<a href='" + fileName);
result.append(sufFix + "?" + reqParam + "&page=");
result.append((currentPage + cou));
result.append("'>" + (currentPage + cou) + "</a>");
result.append(blankStr);
}
result.append("</span>");
}
if (pageCount > INDEX_PAGE_NUM) {
result.append("<span class='pagelink'>");
if (currentPage < pageCount) {
result.append("<a href='" + fileName);
result.append(sufFix + "?" + reqParam + "&page=");
result.append((currentPage + 1) + "'>下一页</a>");
}
result.append(blankStr);
result.append("<a href='" + fileName);
result.append(sufFix + "?" + reqParam + "&page=");
result.append(pageCount + "'>末页</a>");
result.append("</span>");
}
return result.toString();
}
public static void main(String[] args){
System.out.println(getParamPageStr(2,1,3,"555.shtml",".shtml"));
}
}