分页翻页模板

1. 效果图

  其中首页只要查询第一页即可,复杂的是如何计算每次(或不同产品)的上一页、中间页及尾页。其中有两种思路:1、每次在jsp页码计算;2.由后台计算,通过request提供给前端,这篇文章后续主要介绍如何通过后台生成这些数据。

  其中在前端计算这些数据可以参考文章jsp翻页。_kenick的博客-CSDN博客,注意使用时请自己调试下。

2.前台jsp

<div class="page pb15">
	<span class="r inb_a page_b">
	
		<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=1&amp;pageSize=${pageSize}"><font size="2">首页</font></a>
	
		<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=${previous}&amp;pageSize=${pageSize}"><font size="2">上一页</font></a>

        <c:forEach var="page" items="${showPageList}" >
            <a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=${page}&amp;pageSize=${pageSize}" <c:if test="${page == pageNo}">style="background-color: #3399CC"</c:if>>${page}</a>
        </c:forEach>
	
		<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=${nextPage}&amp;pageSize=${pageSize}"><font size="2">下一页</font></a>
	
		<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=${totalPage}&amp;pageSize=${pageSize}"><font size="2">尾页</font></a>
	
		共<c:out value="${totalPage}"/>页
		到第<input type="text" size="2" id="PAGENO" style="width: 20px;" />页 <input type="button" onclick="window.location.href = '<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageSize=${pageSize}&amp;pageNo=' + $('#PAGENO').val() " value="确定" class="hand btn60x20" id="skip"/>
        每页显示
        <select id="pageSize" onchange="window.location.href = '<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&amp;name=${queryName}&amp;pageNo=${pageNo}&amp;pageSize='+$('#pageSize').val()">
            <option value="5" <c:if test="${pageSize == 5}">selected="selected"</c:if>>5</option>
            <option value="10" <c:if test="${pageSize == 10}">selected="selected"</c:if>>10</option>
            <option value="15" <c:if test="${pageSize == 15}">selected="selected"</c:if>>15</option>
            <option value="20" <c:if test="${pageSize == 20}">selected="selected"</c:if>>20</option>
        </select>
	</span>
</div>

3.后台生成上一页、中间页、下一页、尾页工具类。 

package com.kenick.sport;

import java.util.ArrayList;
import java.util.List;

public class PaginationUtil {
    private  Integer pageNo = 1; // 当前页
    private  Integer pageSize = 5; // 每页大小
    private  Integer totalSize; // 总数量

    public PaginationUtil(){}

    public PaginationUtil(Integer pageNo,Integer pageSize,Integer totalSize){
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.totalSize = totalSize;
    }

    /**
     *  获取最后一页
     * @return
     */
    public  Integer getLastPage(){
        int lastPage = 1; // 默认第一页
        if(totalSize > pageSize){
            lastPage = (totalSize % pageSize == 0?(totalSize/pageSize):(totalSize/pageSize+1));
        }
        return lastPage;
    }

    /**
     *  获取上一页
     * @return
     */
    public Integer getPrePage(){
        int prePage = 1;
        if(pageNo > 1){
            prePage = pageNo -1;
        }
        return prePage;
    }

    /**
     *  获取下一页
     * @return
     */
    public Integer getNextPage(){
        Integer lastPage = this.getLastPage();
        return  lastPage>pageNo?pageNo+1:pageNo;
    }

    /**
     *  获取中间num页
     * @return 中间页码字符串,以逗号分隔 2,3,4,5,
     */
    public String getCenterPageStr(Integer num){
        String centerPages = "";
        int startPage = 1; // 起始页码
        int endPage = 1; // 终止页码

        int lastPage = this.getLastPage();
        int nextPart = lastPage - pageNo; // 后续页码
        if(nextPart >= (num-1)){ // 后续页码数大于等于num-1
            startPage = pageNo;
            endPage = pageNo + (num-1);
        }else{ // 后续页码数小于num-1
            startPage = (pageNo - (num-nextPart-1)) >0 ?(pageNo - (num-nextPart -1)) :1;
            endPage = lastPage;
        }

        for(int i=startPage;i<=endPage;i++){
            centerPages = centerPages + i + ",";
        }
        return centerPages;
    }

    /**
     *  获取中间num页
     * @return 中间页码字符串,以逗号分隔 2,3,4,5,
     */
    public List<String> getCenterPageList(Integer num){
        List<String> centerPages = new ArrayList<>();
        int startPage = 1; // 起始页码
        int endPage = 1; // 终止页码

        int lastPage = this.getLastPage();
        int nextPart = lastPage - pageNo; // 后续页码
        if(nextPart >= (num-1)){ // 后续页码数大于等于num-1
            startPage = pageNo;
            endPage = pageNo + (num-1);
        }else{ // 后续页码数小于num-1
            startPage = (pageNo - (num-nextPart-1)) >0 ?(pageNo - (num-nextPart -1)) :1;
            endPage = lastPage;
        }

        for(int i=startPage;i<=endPage;i++){
            centerPages.add(i+"");
        }
        return centerPages;
    }

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalSize() {
        return totalSize;
    }

    public void setTotalSize(Integer totalSize) {
        this.totalSize = totalSize;
    }

    public static void main(String[] args) {
        int pageNo = 10; // 当前页
        int pageSize = 10; // 每页大小
        int totalSize = 100; // 当前查询数据大小
        PaginationUtil paginationUtil = new PaginationUtil(pageNo, pageSize, totalSize);
        System.out.println("当前页码:"+ pageNo);
        System.out.println("每页显示数:"+pageSize);
        System.out.println("总数据:"+totalSize);
        System.out.println("上一页:" + paginationUtil.getPrePage());
        System.out.println("中间页:"+paginationUtil.getCenterPageStr(5));
        System.out.println("下一页:" + paginationUtil.getNextPage());
        System.out.println("尾页:" + paginationUtil.getLastPage());
    }
}

运行效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kenick

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值