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}&name=${queryName}&pageNo=1&pageSize=${pageSize}"><font size="2">首页</font></a>
<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&name=${queryName}&pageNo=${previous}&pageSize=${pageSize}"><font size="2">上一页</font></a>
<c:forEach var="page" items="${showPageList}" >
<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&name=${queryName}&pageNo=${page}&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}&name=${queryName}&pageNo=${nextPage}&pageSize=${pageSize}"><font size="2">下一页</font></a>
<a href="<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&name=${queryName}&pageNo=${totalPage}&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}&name=${queryName}&pageSize=${pageSize}&pageNo=' + $('#PAGENO').val() " value="确定" class="hand btn60x20" id="skip"/>
每页显示
<select id="pageSize" onchange="window.location.href = '<%=path%>/brand/list.do?isDisplay=${queryIsDisplay}&name=${queryName}&pageNo=${pageNo}&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());
}
}
运行效果: