分析并计算分页需要的参数
总条数 不能计算,需要从数据库中查询
当前页 从页面获得
起始行 (当前页-1)*每页显示条数
每页显示条数 固定10条
总页数 总条数%每页显示条数==0?总条数/每页显示条数:总条数/每页显示条数+1
首页 1
尾页 总页数
上一页 当前页==首页?首页:当前页-1
下一页 当前页==尾页?尾页:当前页+1
起始导航
结束导航
靠近首页(当前页<=6)
起始导航 首页
结束导航 10
靠近尾页(当前页+4>=尾页)
起始导航 尾页-9
结束导航 尾页
一般情况
起始导航 当前页-5
结束导航 当前页+4
// 总条数
private int count;
// 当前页
private int curPage;
// 每页显示条数
private int pageRow=10;
// 起始行
private int startRow;
// 总页数
private int pageCount;
// 首页
private int firstPage=1;
// 尾页
private int lastPage;
// 上一页
private int prevPage;
// 下一页
private int nextPage;
// 起始导航
private int beginNav;
// 结束导航
private int endNav;
配置所需要的JavaBean
package com.cdsxt.util;
public class PageUtil {
// 总条数
private int count;
// 当前页
private int curPage;
// 每页显示条数
private int pageRow=10;
// 起始行
private int startRow;
// 总页数
private int pageCount;
// 首页
private int firstPage=1;
// 尾页
private int lastPage;
// 上一页
private int prevPage;
// 下一页
private int nextPage;
// 起始导航
private int beginNav;
// 结束导航
private int endNav;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getStartRow() {
return startRow;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public int getPageRow() {
return pageRow;
}
public void setPageRow(int pageRow) {
this.pageRow = pageRow;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getFirstPage() {
return firstPage;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
public int getLastPage() {
return lastPage;
}
public void setLastPage(int lastPage) {
this.lastPage = lastPage;
}
public int getPrevPage() {
return prevPage;
}
public void setPrevPage(int prevPage) {
this.prevPage = prevPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getBeginNav() {
return beginNav;
}
public void setBeginNav(int beginNav) {
this.beginNav = beginNav;
}
public int getEndNav() {
return endNav;
}
public void setEndNav(int endNav) {
this.endNav = endNav;
}
}
具体实现逻辑
public PageUtil(int count,int curPage){
this.count=count;
this.curPage=curPage;
this.startRow=(curPage-1)*this.pageRow;
this.pageCount=count%this.pageRow==0?count/this.pageRow:count/this.pageRow+1;
this.lastPage=this.pageCount;
this.prevPage=curPage==this.firstPage?this.firstPage:curPage-1;
this.nextPage=curPage==this.lastPage?this.lastPage:curPage+1;
if(this.pageCount<10){
this.beginNav=this.firstPage;
this.endNav=this.lastPage;
}else{
if(curPage<=6){
this.beginNav=this.firstPage;
this.endNav=10;
}else if(curPage+4>=this.lastPage){
this.beginNav=this.lastPage-9;;
this.endNav=this.lastPage;
}else{
this.beginNav=curPage-5;;
this.endNav=curPage+4;
}
}
}
基于封装的分页进行测试
页面
<c:if test="${page.curPage!=page.firstPage }">
<a href="employeeServlet?mark=query&curPage=${page.firstPage }">首页</a>
<a href="employeeServlet?mark=query&curPage=${page.prevPage }">上一页</a>
</c:if>
<c:forEach begin="${page.beginNav }" end="${page.endNav }" var="i" >
<c:choose>
<c:when test="${i==page.curPage }">
<font color="red">${i }</font>
</c:when>
<c:otherwise>
<a href="employeeServlet?mark=query&curPage=${i }">${i }</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${page.curPage!=page.lastPage }">
<a href="employeeServlet?mark=query&curPage=${page.nextPage}">下一页</a>
<a href="employeeServlet?mark=query&curPage=${page.lastPage }">尾页</a>
</c:if>
逻辑代码(action)
public void queryEmployees(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//查询员工的总条数
int count=empDao.queryEmpCount();
//获取当前页
int curPage=request.getParameter("curPage")==null?1:Integer.parseInt(request.getParameter("curPage"));
//创建分页工具对象
PageUtil page=new PageUtil(count, curPage);
int startRow=page.getStartRow();
int pageRow=page.getPageRow();
//查询数据库中的员工数据
List<Employee> empList=empDao.queryEmployees(startRow,pageRow);
request.setAttribute("page", page);
request.setAttribute("empList", empList);
request.getRequestDispatcher("success.jsp").forward(request, response);
return;
}
查询时使用分页limit
"select * from employee limit ?,?"