SpringMVC自定义分页

后台数据

Controller

@RequestMapping(value = "/page", method = RequestMethod.GET)
    public String page(final Model model,
                         @RequestParam(value = "page", required = false, defaultValue = "1") final int pageNum,
                         @RequestParam(value = "pageSize", required = false, defaultValue = "1") final int pageSize) {
        //模拟数据限定pageNum范围为1-6
        //模拟数据限定pageSize值为1
        List results = new ArrayList<>();
        results.add("数据1");
        results.add("数据2");
        results.add("数据3");
        results.add("数据4");
        results.add("数据5");
        results.add("数据6");
        final SearchPageData data = new SearchPageData<>();
        data.setPageNo(pageNum);
        data.setPageSize(pageSize);
        data.setResults(Collections.singletonList(results.get(pageNum-1)));
        data.setTotalRecords(results.size());

        model.addAttribute("pageData", data);
        return "page";
    }

SearchPageData

/**
 * 
 */
package xxx.xxx.xxx;

import java.util.List;

/**
 * @author Administrator
 *
 */
public class SearchPageData<T> {
    private int pageStep = 4;

    private int pageNo;

    private int pageSize;

    private int totalRecords;

    private List results;

    /**
     * @return the pageNo
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * @param pageNo the pageNo to set
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the totalRecords
     */
    public int getTotalRecords() {
        return totalRecords;
    }

    /**
     * @param totalRecords the totalRecords to set
     */
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    /**
     * @return the results
     */
    public List getResults() {
        return results;
    }

    /**
     * @param results the results to set
     */
    public void setResults(List results) {
        this.results = results;
    }

    public int getFirstPageNo() {
        return 1;
    }

    public int getPreviousPageNo() {
        if (pageNo <= 1) {
            return 1;
        }
        return pageNo -1;
    }

    public int getNextPageNo() {
        if (pageNo >= getTotalPages()) {
            return getTotalPages() == 0 ? 1 : getTotalPages();
        }
        return pageNo + 1;
    }

    public int getLastPageNo() {
        return getTotalPages() == 0 ? 1 : getTotalPages();
    }

    /* getDisableFirst() also is ok */
    public boolean isDisableFirst() {
        return pageNo == getFirstPageNo();
    }

    /* getDisableLast() also is ok */
    public boolean isDisableLast() {
        return pageNo == getLastPageNo();
    }

    /* getEllipsisBefore() also is ok */
    public boolean isEllipsisBefore() {
        return getBegin() != getFirstPageNo();
    }

    /* getEllipsisAfter() also is ok */
    public boolean isEllipsisAfter() {
        return getEnd() != getLastPageNo();
    }

    public int getTotalPages() {
        return totalRecords == 0 ? 1 : (totalRecords + pageSize - 1) / pageSize;
    }

    public int getBegin() {
        if (pageNo > pageStep + 1 && getTotalPages() > 2 * pageStep + 1) {
            return pageNo - pageStep;
        }
        return getFirstPageNo();
    }

    public int getEnd() {
        if (getTotalPages() > 2 * pageStep + 1) {
            if (pageNo > getTotalPages() - pageStep && pageNo <= getTotalPages()) {
//              return getLastPageNo();
            } else if (pageNo > pageStep + 1 && pageNo <= getTotalPages() - pageStep) {
                return pageNo + pageStep;
            } else {
                return 2 * pageStep + 1;
            }
        } 
        return getLastPageNo();
    }

}

页面

page.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="pager" tagdir="/WEB-INF/tags" %>

if test="${not empty pageData.results }">
    forEach items="${pageData.results}" var="item">
        ${item}
    forEach>
    "${pageData }"/>
if>

/WEB-INF/tags/pager.tag

<%@ tag pageEncoding="utf-8" %>
<%@ tag body-content="empty" trimDirectiveWhitespaces="true" %>
<%@ attribute name="pager" required="true" type="com.example.data.SearchPageData" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<p class="text-muted text-right">${pager.pageNo } / ${pager.totalPages } 页,共 ${pager.totalRecords } 条记录p>
<ul class="pagination"> 
    <li class="${pager.disableFirst?'disabled':'' }">
        <%-- ""/>">首页 --%>
        <a href="?page=${pager.firstPageNo }">首页a>
    li>
    <li class="${pager.disableFirst?'disabled':'' }">
        <%-- ""/>" --%>
        <a href="?page=${pager.previousPageNo }">«a>
    li> 
    <li class="${pager.ellipsisBefore?'':'hidden' }">
        <a href="#" style="pointer-events: none;">...a>
    li>
    <c:forEach var="pageNo" begin="${pager.begin }" end="${pager.end }">
        <li class="${pager.pageNo==pageNo?'active':'' }">
            <%-- ""/>">${pageNo } --%>
            <a href="?page=${pageNo }">${pageNo }a>
        li> 
    c:forEach>
    <li class="${pager.ellipsisAfter?'':'hidden' }">
        <a href="#" style="pointer-events: none;">...a>
    li>
    <li class="${pager.disableLast?'disabled':'' }">
        <%-- ""/>" --%>
        <a href="?page=${pager.nextPageNo }">»a>
    li> 
    <li class="${pager.disableLast?'disabled':'' }">
        <%-- ""/>">尾页 --%>
        <a href="?page=${pager.lastPageNo }">尾页a>
    li>
ul>

效果

这里写图片描述

这里写图片描述

这里写图片描述

你可能感兴趣的:(SpringMVC)