PageHelper 官网 https://pagehelper.github.io/
1.在 pom 文件中引入 PageHelper 依赖
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency>
2.在 mybatis-config 中引入 PageHelper 插件
<plugin interceptor="com.github.pagehelper.PageInterceptor"> </plugin>
3.Controller
public ModelAndView home(@RequestParam(value="pn",defaultValue="1")Integer pn)
//在查询之前调用 //显示的页码,每页显示的个数 PageHelper.startPage(pn,5); //startPage 后紧跟一个查询 List list = this.adminFacade.getAllTeacher(pd); //使用 PageInfo 包装 PageInfo page = new PageInfo(list); mv.addObject("teachers", page);
PageInfo 提供的方法
4.页面
引入 taglib
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
初始化 页面逻辑(地址为 Controller 的 RequestMapping 地址)
<table>
<c:forEach items="${teachers.list}" var="teacher">
<tr>
<td>${teacher.tname}</td>
<td>${teacher.fullname }</td>
</tr>
</table>
//页码逻辑
<ul> <li><a href="<%=basePath%>address/message?pn=1">首页</a></li> <c:if test="${pageInfo.hasPreviousPage}"> <li class="prev disabled"><a href="<%=basePath%>address/message?pn=${pageInfo.pageNum-1}">«</a></li> </c:if> <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num"> <c:if test="${page_Num==pageInfo.pageNum }"> <li class="active"><a href="#">${page_Num}</a></li> </c:if> <c:if test="${page_Num!=pageInfo.pageNum }"> <li><a href="<%=basePath%>address/message?pn=${page_Num}">${page_Num}</a></li> </c:if> </c:forEach> <c:if test="${pageInfo.hasNextPage}"> <li class="next"><a href="<%=basePath%>address/message?pn=${pageInfo.pageNum+1}">»</a></li> </c:if> <li><a href="<%=basePath%>address/message?pn=${pageInfo.pages}">末页</a></li> </ul>
或 ajax 调用
function userLogin(URL,HREF) { var data = $(".login").serialize(); $.ajax({ url : URL, type : "post", data : data, dataType : 'json', success : function(data) { if (data.check == "true") {
//前往的地址 window.location.href =abroot + HREF; } else { alert("用户名或口令输入错误"); } }, error : function(data) { alert("用户名或口令输入错误"); } }); }