闲来无事, 自己写了一个分页查询, 可以提供初学者作为参考
1.首先封装一个分页对象, 通常情况下, 分页需要封装以下几个参数:
package com.crud.utils;
import java.util.List;
public class PageBean {
private int currPage; //当前页
private int totalPage; //总页数
private int count; //总记录数
private int pageSize; //每页显示数量
private List<Customer> list; //用于存储每页显示的记录Customer对象的个数
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<Customer> getList() {
return list;
}
public void setList(List<Customer> list) {
this.list = list;
}
}
2 , 通过查询数据库, 我们可以把需要的数据封装到PageBean的对象中
public PageBean findCustomersByPage(int currPage) throws Exception {
//当前页 currPage
//自己指定每页显示多少数量的数据
int pageSize = 15;
//数据库查询出总记录数
int count = cd.findCustomersCount();
//转成double类型, 方便调用Math函数处理总页数,仅此而已
double totalCount = count;
//总页数
int totalPage = (int) Math.ceil(totalCount / pageSize);
//mysql中 使用limit查询的参数第一个为 起始记录数; 第二个为 每页的数量
int begin = (currPage - 1) * pageSize;
List<Customer> list = cd.findCustomerByPage(begin , pageSize);
//封装
PageBean pg = new PageBean();
pg.setCurrPage(currPage);
pg.setPageSize(pageSize);
pg.setCount(count);
pg.setTotalPage(totalPage);
pg.setList(list);
return pg;
}
3, 前端代码
<tr align="center">
<td colspan="9">
第<font style="color: blue">${pageBean.currPage }/${pageBean.totalPage }</font>页
每页显示<font style="color: blue">${pageBean.pageSize }</font>条
总记录数<font style="color: blue">${pageBean.count }</font>条
<c:if test="${pageBean.currPage != 1 }"> <!-- 判断当前页是否第一页,若不是第一页,就显示首页和下一页,反之不显示 -->
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=1" style="text-decoration: none">首页</a>
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${pageBean.currPage - 1}" style="text-decoration: none">前一页 </a>
</c:if>
<c:if test="${pageBean.totalPage <= 9 }"> <!-- 分页显示页码这里选择的是9,若是总页数小于等于9,则全部显示 -->
<c:forEach begin="1" var="i" end="${pageBean.totalPage }">
<c:if test="${pageBean.currPage == i }">
<font style="color: red">${i }</font>
</c:if>
<c:if test="${pageBean.currPage != i }"> <!-- 选择你要去的页数,假如当前页是4,然后点击6,此时i=6,点击后跳转第6页 -->
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${i }" style="text-decoration: none">${i } </a>
</c:if>
</c:forEach>
</c:if>
<c:if test="${pageBean.totalPage > 9 }"><!-- 当总页数大于9的时候 -->
<c:if test="${pageBean.currPage <= 5 }"> <!-- 若是当前页小于5,则显示9个页码后用...代替,表示后面还有 -->
<c:forEach var="i" begin="1" end="9">
<c:if test="${pageBean.currPage == i }">
<font style="color: red">${i }</font>
</c:if>
<c:if test="${pageBean.currPage != i }">
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${i }" style="text-decoration: none">${i } </a>
</c:if>
</c:forEach>
...
</c:if>
<c:if test="${pageBean.currPage > 5 }"> <!-- 若是当前页大于5,则需要进行下面的判断 -->
<c:if test="${pageBean.currPage + 4 < pageBean.totalPage }"> <!-- 若是这种情况,则为中间显示9个页码,前后均为... -->
...
<c:forEach var="i" begin="${pageBean.currPage - 4 }" end="${pageBean.currPage + 4 }">
<c:if test="${pageBean.currPage == i }">
<font style="color: red">${i }</font>
</c:if>
<c:if test="${pageBean.currPage != i }">
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${i }" style="text-decoration: none">${i } </a>
</c:if>
</c:forEach>
...
</c:if>
<c:if test="${pageBean.currPage + 4 >= pageBean.totalPage }"><!-- 若是这种情况,只需前面加上...即可 -->
...
<c:forEach var="i" begin="${pageBean.totalPage - 8 }" end="${pageBean.totalPage }">
<c:if test="${pageBean.currPage == i }">
<font style="color: red">${i }</font>
</c:if>
<c:if test="${pageBean.currPage != i }">
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${i }" style="text-decoration: none">${i } </a>
</c:if>
</c:forEach>
</c:if>
</c:if>
</c:if>
<c:if test="${pageBean.currPage != pageBean.totalPage }"> <!-- 此情况和显示首页前一页类似,不再赘述 -->
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${pageBean.currPage + 1 }" style="text-decoration: none">后一页</a>
<a href="${pageContext.request.contextPath }/FindCustomerByPage?currPage=${pageBean.totalPage }" style="text-decoration: none">末页</a>
</c:if>
</td></tr>
至此,分页查询功能全部完成 ,下面我们对分页进行抽取, 提取出更方便的分页工具类 ,读者可以直接调用此分页工具类, 通过设置路径即可完成分页功能
1, 工具类
package com.crud.utils;
import java.util.List;
public class PageBean<T> {
//注意:1,在Action层设置url 即action层的访问路径 2,前端页面的取值为 ${pageBean.links }
private int currPage; //当前页
private int totalPage; //总页数
private int count; //总记录数
private int pageSize; //每页显示数量
private List<T> list; //用于存储每页显示的记录
public int getCurrPage() {
return currPage;
}
public void setCurrPage(int currPage) {
this.currPage = currPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String links;
public String getLinks(){
int currentPage = this.currPage;
StringBuffer sb = new StringBuffer();
sb.append("第<font style=\"color: blue\">"+this.getCurrPage()+"/"+this.getTotalPage()+"</font>页 ");
sb.append("每页显示<font style=\"color: blue\">"+this.getPageSize()+"</font>条 ");
sb.append("总记录数<font style=\"color: blue\">"+this.getCount()+"</font>条 ");
if (this.getCurrPage() <= 1) {
currentPage = 1 ;
} else {
currentPage = this.currPage - 1;
}
if (this.getCurrPage() != 1) {
sb.append("<a href=\""+this.url+"?currPage=1\" style=\"text-decoration: none\">首页 </a>");
sb.append("<a href=\""+this.url+"?currPage="+ currentPage+ "\" style=\"text-decoration: none\">前一页 </a>");
}
if (this.getTotalPage() <= 9) {
for (int i = 1; i <= this.getTotalPage(); i++) {
if (this.getTotalPage()==i) {
sb.append("<font style=\"color: red\">"+i+"</font> ");
} else {
sb.append("<a href=\""+this.url+"?currPage="+ i +"\" style=\"text-decoration: none\">"+i+" </a>");
}
}
}
if (this.getTotalPage() > 9) {
if (this.getCurrPage() <= 5) {
for (int i = 1; i <= 9; i++) {
if (this.getCurrPage() == i) {
sb.append("<font style=\"color: red\">"+ i +"</font> ");
} else {
sb.append("<a href=\""+this.url+"?currPage="+ i +"\" style=\"text-decoration: none\">"+ i +" </a>");
}
}
sb.append("...");
} else {
if (this.getCurrPage() + 4 < this.getTotalPage()) {
sb.append("...");
for (int i = this.getCurrPage() - 4; i <= this.getCurrPage() + 4; i++) {
if (this.getCurrPage()==i) {
sb.append("<font style=\"color: red\">"+ i +"</font> ");
} else {
sb.append("<a href=\""+this.url+"?currPage="+ i +"\" style=\"text-decoration: none\">"+ i +" </a>");
}
}
sb.append("...");
} else {
sb.append("...");
for (int i = this.getTotalPage() - 8; i <= this.getTotalPage(); i++) {
if (this.getCurrPage()==i) {
sb.append("<font style=\"color: red\">"+ i +"</font> ");
} else {
sb.append("<a href=\""+this.url+"?currPage="+ i +"\" style=\"text-decoration: none\">"+ i +" </a>");
}
}
}
}
}
if (this.getCurrPage() >= this.getTotalPage() ) {
currentPage = this.getTotalPage();
} else {
currentPage = this.getCurrPage() + 1;
}
if (this.getCurrPage() != this.getTotalPage()) {
sb.append("<a href=\""+this.url+"?currPage="+currentPage+" \" style=\"text-decoration: none\"> 后一页 </a>");
sb.append("<a href=\""+this.url+"?currPage="+this.getTotalPage()+"\" style=\"text-decoration: none\">末页</a>");
}
links = sb.toString();
return links;
}
}
2, 业务层查询数据库, 进行封装
public PageBean findCustomerByPage(int currPage) throws Exception {
//当前页 currPage
//指定每页显示多少数量的数据
int pageSize = 15;
//数据库查询总记录数
int count = cd.findCustomerCount();
//总页数
double totalCount = count;
//总页数
int totalPage = (int) Math.ceil(totalCount / pageSize);
int begin = (currPage - 1) * pageSize;
List<Customer> list = cd.findCustomerByPage(begin , pageSize);
PageBean pg = new PageBean();
pg.setCurrPage(currPage);
pg.setPageSize(pageSize);
pg.setCount(count);
pg.setTotalPage(totalPage);
pg.setList(list);
return pg;
}
3, action层只需对工具类进行地址设置
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
int currPage = Integer.parseInt(request.getParameter("currPage"));
CustomerService cs = new CustomerServiceImpl();
PageBean pb = null;
pb = cs.findCustomerByPage(currPage);
pb.setUrl("/test_geode/FindCustomerByPage"); //设置uri为你的项目访问相关action的地址即可
request.setAttribute("pageBean", pb);
request.getRequestDispatcher("/crud/showCustomer.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
4, 前端页面调用方式 : 在你想要调用的位置,书写下面一行代码即可实现
${pageBean.links }