实体类:
package com.adsm.common.util;
/**
* 分页信息
* Created by hu on 2016/1/17.
*/
public class Page {
public final static int PAGESIZE = 2;
private int pageSize = PAGESIZE; // 每页条数
private int pageCount = 4; // 最多显示的总页数
private int totalCount = 0; // 总记录数
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
}
admin.jsp(显示页面)
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager"%>
<!--
totalCount //总记录数
maxPageItems //每页记录数
maxIndexPages //总页数
-->
<pg:pager url="${pageContext.request.contextPath}/userController/userList" items="${page.totalCount}" maxPageItems="${page.pageSize}" maxIndexPages="${page.pageCount}" export="offset,currentPageNumber=pageNumber" scope="request" >
<jsp:include page="../common/page.jsp"/>
</pg:pager>
page.jsp(包含页面)
<%--
Created by IntelliJ IDEA.
User: hu
Date: 2016/1/17
Time: 9:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="pg" uri="http://jsptags.com/tags/navigation/pager"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<ul class="pagination">
<pg:index>
<pg:first>
<li><a href="${pageUrl }">«</a></li>
</pg:first>
<!-- 首页 -->
<pg:first>
<!-- 生成链接 -->
<li><a href="${pageUrl }">首页</a></li>
</pg:first>
<pg:prev>
<li><a href="${pageUrl }">前一页</a></li>
</pg:prev>
<pg:pages>
<c:choose>
<c:when test="${currentPageNumber eq pageNumber }">
<li class="active"><a>${pageNumber }</a></li>
</c:when>
<c:otherwise>
<li><a href="${pageUrl }">${pageNumber }</a><li>
</c:otherwise>
</c:choose>
</pg:pages>
<pg:next>
<li><a href="${pageUrl }">后一页</a></li>
</pg:next>
<pg:last>
<li><a href="${pageUrl }">尾页</a></li>
</pg:last>
<pg:last>
<li><a href="${pageUrl }">»</a></li>
</pg:last>
</pg:index>
</ul>
controller:
/**
* 显示用户列表
*/
@RequestMapping(value = "/userList", method = RequestMethod.GET)
public String userList(HttpServletRequest request, HttpServletResponse response, Map<String, Object> map) {
String offset = request.getParameter("pager.offset");// 相当于原来的(cpage-1)*pageSize
if(offset == null || offset.length() == 0){
offset = "0";
}
logger.info("offset:" + offset);
SUserCore sUserCore = new SUserCore();
sUserCore.setEnable((byte) 1);
sUserCore.setStatus((byte) 0);
List<SUserCore> totalUserCores = sUserCoreService.selectList(sUserCore);
Map<String, Object> pageMap = new HashMap<>();
pageMap.put("enable", (byte) 1);
pageMap.put("status", (byte) 0);
pageMap.put("cpage", Integer.parseInt(offset));
pageMap.put("pageSize", Page.PAGESIZE);
List<SUserCore> sUserCores = sUserCoreService.selectListForPage(pageMap);
// 分页
Page page = new Page();
page.setTotalCount(totalUserCores.size());
logger.info("page:" + JacksonJsonUtil.beanToJson(page));
map.put("users", sUserCores);
map.put("page", page);
return "admin";
}