import javax.servlet.http.HttpServletRequest;
/**
* 该类主要是用于分页。
* @author 邬小洪
* 2009.01.02
*/
public class PagesUtil {
//总记录条数
private int totalRecords=0;
//总页面数
private int totalPages=0;
//当前页数
private int currentPage=0;
//是否初始化
private boolean inited=false;
/**
* 初始化参数
* @param request 页面请求
* @param allRecord 总记录数
* @param pageRows 每页显示的记录数
*/
public void init(HttpServletRequest request,int totalRecordsIn,int pageRows){
totalRecords = totalRecordsIn;
//获得总页面数
totalPages = (totalRecords + pageRows - 1) / pageRows;
//判断参数currentPage是否为空
String currentPageStr = request.getParameter("currentPage");
if (currentPageStr == null||currentPageStr.length()==0)
currentPage = 0;//初始值为0
else
currentPage = Integer.parseInt(currentPageStr);
//判断当前页
if (currentPage < 0) {
currentPage = 0;
} else if (currentPage > totalPages - 1 && totalPages >= 1) {
currentPage = totalPages - 1;
}
inited=true;
}
/**
* 返回分页的结果.
* @param recordPages 当前页前后显示的页面数
* @param urlParam 提交页面地址
* @return 1 2 3 4 5 6 7 8 9 10 11 共332条 共34页 第[6]页 首页 上一页 下一页 尾页
*/
public String getPages(int recordPages,String urlParam){
if(!inited){
System.out.println("请先调用初始化方法:init(HttpServletRequest request,int allRecord,int pageRows)");
return "";
}
StringBuffer pageBuffer = new StringBuffer();
if(totalRecords!=0){
for (int n = 0; n < totalPages; n++){
if(n<=(currentPage+recordPages)&&n>=(currentPage-recordPages)){
if(currentPage==n){
pageBuffer.append("<font color='red'>");
pageBuffer.append(n+1);
pageBuffer.append("</font>");
}else{
pageBuffer.append("<a href='");
pageBuffer.append(urlParam);
pageBuffer.append("¤tPage=");
pageBuffer.append(n);
pageBuffer.append("'>");
pageBuffer.append(n+1);
pageBuffer.append("</a>");
}
pageBuffer.append(" ");
}
}
pageBuffer.append(" ");
pageBuffer.append("共");
pageBuffer.append(totalRecords);
pageBuffer.append("条");
pageBuffer.append(" ");
pageBuffer.append("共");
pageBuffer.append(totalPages);
pageBuffer.append("页");
pageBuffer.append(" ");
pageBuffer.append("第[<font color='red'>");
pageBuffer.append(currentPage+1);
pageBuffer.append("</font>]页");
pageBuffer.append(" ");
if(totalPages>1){
// 首页
if(totalPages>1&¤tPage>0){
pageBuffer.append("<a href='");
pageBuffer.append(urlParam);
pageBuffer.append("¤tPage=0");
pageBuffer.append("'>首页</a>");
}else{
pageBuffer.append("首页");
}
pageBuffer.append(" ");
//上一页
if(currentPage>0){
pageBuffer.append("<a href='");
pageBuffer.append(urlParam);
pageBuffer.append("¤tPage=");
pageBuffer.append(currentPage-1);
pageBuffer.append("'>上一页</a>");
}else{
pageBuffer.append("上一页");
}
pageBuffer.append(" ");
//下一页
if(currentPage<totalPages-1){
pageBuffer.append("<a href='");
pageBuffer.append(urlParam);
pageBuffer.append("¤tPage=");
pageBuffer.append(currentPage+1);
pageBuffer.append("'>下一页</a>");
}else{
pageBuffer.append("下一页");
}
pageBuffer.append(" ");
//尾页
if(totalPages>1&¤tPage<totalPages-1){
pageBuffer.append("<a href='");
pageBuffer.append(urlParam);
pageBuffer.append("¤tPage=");
pageBuffer.append(totalPages-1);
pageBuffer.append("'>尾页</a>");
}else{
pageBuffer.append("尾页");
}
}
}
return pageBuffer.toString();
}
/**
* 获取当前页面数
* @return 当前页面数
*/
public int getCurrentPage() {
if(!inited){
System.out.println("请先调用初始化方法:init(HttpServletRequest request,int allRecord,int pageRows)");
return 0;
}
return currentPage;
}
}
使用方法:创建test.jsp
<%@ page pageEncoding="UTF-8"%>
<jsp:directive.page import="*.PagesUtil"/>
<%
PagesUtil pu = new PagesUtil();
pu .init(request,10000,10);
String urlParam = "./test.jsp?";
%>
<%=pu .getPages(5,urlParam) %>