1、建立一个Page类
public class Page {
private List records;
private int currentPageNum;
private int pageSize=10 ;
private int totalRecords;
private int totalPage;
private int startIndex;
private int prePageNum;
private int nextPageNum;
private int startPage;
private int endPage;
private String uri;
public String getUri () {
return uri;
}
public void setUri (String uri) {
this .uri = uri;
}
public Page (int currentPageNum,int totalRecords)
{
this .currentPageNum=currentPageNum;
this .totalRecords=totalRecords;
/**
* 记录的总页数
*/
totalPage=totalRecords%pageSize==0 ?totalRecords%pageSize:totalRecords%pageSize+1 ;
startIndex=(currentPageNum-1 )*10 ;
if (totalPage>5 )
{
startPage=currentPageNum-2 ;
endPage=currentPageNum+2 ;
if (startPage<1 )
{
startPage=1 ;
endPage=5 ;
} else if (endPage>totalPage)
{
endPage=totalPage;
startPage=totalPage-4 ;
}
}else
{
startPage=1 ;
endPage=totalPage;
}
}
public List getRecords () {
return records;
}
public void setRecords (List records) {
this .records = records;
}
public int getCurrentPageNum () {
return currentPageNum;
}
public void setCurrentPageNum (int currentPageNum) {
this .currentPageNum = currentPageNum;
}
public int getPageSize () {
return pageSize;
}
public void setPageSize (int pageSize) {
this .pageSize = pageSize;
}
public int getTotalRecords () {
return totalRecords;
}
public void setTotalRecords (int totalRecords) {
this .totalRecords = totalRecords;
}
public int getTotalPage () {
return totalPage;
}
public void setTotalPage (int totalPage) {
this .totalPage = totalPage;
}
public int getStartIdex () {
return startIndex;
}
public void setStartIdex (int startIdex) {
this .startIndex = startIdex;
}
public int getPrePageNum () {
prePageNum=currentPageNum-1 ;
if (prePageNum<1 )
prePageNum=1 ;
return prePageNum;
}
public void setPrePageNum (int prePageNum) {
this .prePageNum = prePageNum;
}
public int getNextPageNum () {
nextPageNum=currentPageNum+1 ;
if (nextPageNum>totalPage)
nextPageNum=totalPage;
return nextPageNum;
}
public void setNextPageNum (int nextPageNum) {
this .nextPageNum = nextPageNum;
}
public int getStartPage () {
return startPage;
}
public void setStartPage (int startPage) {
this .startPage = startPage;
}
public int getEndPage () {
return endPage;
}
public void setEndPage (int endPage) {
this .endPage = endPage;
}
}
2、查询数据的总数目
public int getTotalRecords ()throws Exception;
3、返回限制查询结果集
public List<类名> findPageRecords (int startIndex,int pageSize)throws Exception;
4、用页数返回结果
public Page findPage (String num)throws Exception;
public Page findPage (String num) throws Exception {
int pageNum=1 ;
if (num!=null &&!"" .equals(num))
{
pageNum=Integer.parseInt(num);
}
System.out.println("pageNum==" +pageNum);
int totalRecords=groupDao.getTotalRecords();
Page page=new Page(pageNum, totalRecords);
List records=groupDao.findPageRecords(page.getStartIdex(),page.getPageSize());
page.setRecords(records);
return page;
}
5、前端代码
<c:forEach begin ="${page.startPage}" end ="${page.endPage}" var ="num" >
<a href ="${pageContext.request.contextPath}${page.uri}&num=${num}" > ${num}</a >
</c:forEach >
第${page.currentPageNum}页/共${page.totalPage }页
<a href ="${pageContext.request.contextPath}" > 首页</a >
<a href ="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.prePageNum}" > 上一页</a >
<a href ="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.nextPageNum}" > 下一页</a >
<a href ="${pageContext.request.contextPath}/servlet/Controller?op=showAllCustomers&num=${page.totalPage}" > 尾页</a >
<select id ="pagenum" onchange ="jump(this)" >
<c:forEach begin ="1" end ="${page.totalPage}" var ="num" >
<option value ="${num}" ${num ==page.currentPageNum?'selected="selected"': ''}> ${num}</option >
</c:forEach >
</select >