虽然现在已经有很多的分页插件可以直接实现分页功能,例如PageHelper等等。但是还是自己手写了个原生的分页功能。
首先设计PageBean实体类,包含5个字段
// 总记录数
private int totalCount;
// 总页码
private int tltalPage;
// 每页数据
private List<T> list;
// 当前页码
private int currentPage;
// 每页显示页数
private int rows;
首次进入后台页面时,自动传入currentPage和rows (如1,5),调用方法
findUserbyPage
public static PageBean<Student> findUserbyPage(String currentPage, String rows) {
// 创建新的pagebean
PageBean<Student> pb = new PageBean<Student>();
pb.setCurrentPage(Integer.parseInt(currentPage));
pb.setRows(Integer.parseInt(rows));
// 总条数
int totalCount = findtatalCount();
pb.setTotalCount(totalCount);
int start = (Integer.parseInt(currentPage)-1) * Integer.parseInt(rows);
List<Student> list = findByPage(start,Integer.parseInt(rows));
// 每页显示的数据
pb.setList(list);
//总页码
int tltalPage = (totalCount % Integer.parseInt(rows)) ==0 ? totalCount / Integer.parseInt(rows) : totalCount / Integer.parseInt(rows)+1;
pb.setTltalPage(tltalPage);
return pb;
}
findByPage查询每页显示的数据并且封装到list中
public static List<Student> findByPage(int start,int rows) {
String sql = "select * from student limit ? ,?";
return jdbcTemplate.query(sql,new BeanPropertyRowMapper<Student>(Student.class),start,rows);
}
control层
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPage=request.getParameter("currentPage"); // 当前页码
String rows = request.getParameter("rows");
PageBean<Student> pb = StudentDao.findUserbyPage(currentPage,rows);
request.setAttribute("pb",pb);
System.out.println(pb);
request.getRequestDispatcher("/list.jsp").forward(request,response);
}
最后展示在页面上就okay
<c:forEach begin="1" end="${pb.tltalPage}" var="i">
<li><a href="${pageContext.request.contextPath}/findUserbyPage?currentPage=${i}&rows=5" class="active">${i}</a></li>
</c:forEach>
新作一个项目发现另一种方法
pagebean
在po类中直接完成上一页,下一页,页码的设计
package com.system.po;
/**
* 分页信息 pojo类
*/
public class PageBean{
//当前页码,默认第一页
private int curentPageNo = 1;
//总页数
private int totalCount;
//页面容量
private int pageSize=5;
//上一页
private int upPageNo;
//下一页
private int nextPageNo;
//要前往的页码,默认0
private int toPageNo = 0;
public void setToPageNo(Integer toPageNo) {
//新一页
this.toPageNo = (toPageNo-1) * pageSize ;
//设置跳转后当前的页码
setCurentPageNo(toPageNo);
}
public Integer getTopageNo() {
return toPageNo;
}
public int getCurentPageNo() {
return curentPageNo;
}
//设置当前页码
public void setCurentPageNo(int curentPageNo) {
if (curentPageNo != 1) {
this.upPageNo = curentPageNo - 1;
}
this.nextPageNo = curentPageNo + 1;
this.curentPageNo = curentPageNo;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if (totalCount%pageSize > 0) {
this.totalCount = (totalCount/pageSize)+1;
} else {
this.totalCount = totalCount/pageSize;
}
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getUpPageNo() {
return upPageNo;
}
public void setUpPageNo(int upPageNo) {
this.upPageNo = upPageNo;
}
public int getNextPageNo() {
return nextPageNo;
}
public void setNextPageNo(int nextPageNo) {
this.nextPageNo = nextPageNo;
}
}
controller层中完成跳转
// 学生信息显示
@RequestMapping("/showStudent")
public String showStudent(Model model, Integer page) throws Exception {
List<StudentCustom> list = null;
//页码对象
PageBean pagingVO = new PageBean();
//设置总页数 传入一共多少条数据,在set中设置页码
pagingVO.setTotalCount(studentService.getCountStudent());
// 如果是第一次进入跳转第一页
if (page == null || page == 0) {
pagingVO.setToPageNo(1);
list = studentService.findByPaging(1);
}
else {
pagingVO.setToPageNo(page);
list = studentService.findByPaging(page);
}
model.addAttribute("studentList", list);
model.addAttribute("pagingVO", pagingVO);
return "admin/showStudent";
}
jsp
<%--上一页 下一页得设计--%>
<div class="panel-footer">
<c:if test="${pagingVO != null}">
<nav style="text-align: center">
<ul class="pagination">
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.upPageNo}">«上一页</a></li>
<li class="active"><a href="">${pagingVO.curentPageNo}</a></li>
<c:if test="${pagingVO.curentPageNo+1 <= pagingVO.totalCount}">
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.curentPageNo+1}">${pagingVO.curentPageNo+1}</a></li>
</c:if>
<c:if test="${pagingVO.curentPageNo+2 <= pagingVO.totalCount}">
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.curentPageNo+2}">${pagingVO.curentPageNo+2}</a></li>
</c:if>
<c:if test="${pagingVO.curentPageNo+3 <= pagingVO.totalCount}">
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.curentPageNo+3}">${pagingVO.curentPageNo+3}</a></li>
</c:if>
<c:if test="${pagingVO.curentPageNo+4 <= pagingVO.totalCount}">
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.curentPageNo+4}">${pagingVO.curentPageNo+4}</a></li>
</c:if>
<li><a href="${pageContext.request.contextPath}/admin/showStudent?page=${pagingVO.totalCount}">最后一页»</a></li>
</ul>
</nav>
</c:if>
</div>