Skip to content

Instantly share code, notes, and snippets.

@qb20nh
Created December 6, 2021 13:44
Show Gist options
  • Save qb20nh/29dc3f5315be87c290a994eaa109005d to your computer and use it in GitHub Desktop.
Save qb20nh/29dc3f5315be87c290a994eaa109005d to your computer and use it in GitHub Desktop.
Java Pagination Example
package kr.co.benew.util;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class Pagination<T> {
private int pageSize = 10;
private int paginationSize = 9;
private int currentPage = 0;
private List<T> list = null;
private String directory;
public Pagination(List<T> list) {
this.list = list;
}
public int getSize() {
return list == null ? 0 : list.size();
}
public int getOffset() {
return getPageSize() * getCurrentPage();
}
public int getLimit() {
return getPageSize();
}
public List<T> getItemsOnCurrentPage() {
return list.subList(getOffset(), getOffset()+getLimit());
}
public RowBounds toRowBounds() {
return new RowBounds(getOffset(), getLimit());
}
public int getPageCount() {
return getSize() / getPageSize() + (getSize() % getPageSize() > 0 ? 1 : 0);
}
public int getActualPaginationSize() {
return Math.min(getPaginationSize(), getPageCount());
}
public int getLastPage() {
return getPageCount() - 1;
}
public int getStartPage() {
if (getPageCount() > getPaginationSize()
&& getCurrentPage() > getPaginationSize() / 2) {
return Math.min(getCurrentPage() - getPaginationSize() / 2,
getPageCount() - getActualPaginationSize());
} else {
return 0;
}
}
public int getEndPage() {
return Math.min(getStartPage() + getPaginationSize(), getPageCount()) - 1;
}
public void setCurrentPage(int newPage) {
if (newPage < 0) newPage = 0;
if (newPage > getLastPage()) newPage = getLastPage();
this.currentPage = newPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage();
}
public boolean hasPreviousPage() {
return getStartPage() > 0;
}
public boolean isHasNextPage() {
return hasNextPage();
}
public boolean hasNextPage() {
return getEndPage() < getLastPage();
}
public boolean isShouldShowFirst() {
return shouldShowFirst();
}
public boolean shouldShowFirst() {
return getStartPage() - getPaginationSize() / 2 > 0;
}
public boolean isShouldShowLast() {
return shouldShowLast();
}
public boolean shouldShowLast() {
return getEndPage() + getPaginationSize() / 2 < getLastPage();
}
public int getPreviousPage() {
return getCurrentPage() - getPaginationSize();
}
public int getNextPage() {
return getCurrentPage() + getPaginationSize();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<div class="wrapper">
<style scoped>
.pages {
font-size: 0;
--border-width: 1px;
--border-radius: 2px;
--border-color: #94d4b0;
--highlight-bgcolor: #12a653;
--highlight-color: #d3ffa3;
user-select: none;
--white: #fff;
}
li {
display: inline-block;
border: var(--border-width) solid var(--border-color);
box-sizing: border-box;
min-width: 4ch;
padding: 2ex;
font-size: 12px;
height: 36px;
line-height: 36px;
text-align: center;
border-right-width: 0;
position: relative;
}
li:first-of-type {
border-top-left-radius: var(--border-radius);
border-bottom-left-radius: var(--border-radius);
}
li:last-of-type {
border-right-width: var(--border-width);
border-top-right-radius: var(--border-radius);
border-bottom-right-radius: var(--border-radius);
}
li:after {
content: '';
display: inline-block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
outline: 4px solid;
outline-color: var(--highlight-bgcolor);
z-index: 1;
pointer-events: none;
opacity: 0;
transition: all .2s linear;
}
li:active:after, li:focus-within:after {
opacity: 0.4;
}
li>a {
display: inline-block;
position:absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
line-height: 32px;
transition: all .2s linear;
}
li>a:focus-visible {
outline: none;
}
li:not(.page)>a {
line-height: 30px;
color: var(--highlight-bgcolor);
background-color: var(--highlight-color);
font-weight: bolder;
}
li:not(.page)>a:after {
content: attr(data-page);
display: inline-block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
line-height: 32px;
color: transparent;
}
li:not(.page)>a:hover:after,
li:not(.page)>a:focus:after {
color: var(--white);
}
li:not(.page)>a:hover,
li:not(.page)>a:focus {
background-color: var(--highlight-bgcolor);
color: transparent;
}
li.page:not(.active)>a {
cursor: pointer;
color: var(--highlight-bgcolor);
}
li.page:not(.active)>a:hover,
li.page:not(.active)>a:focus {
background-color: var(--highlight-color);
}
li.page.active>a,
li.page:not(.active)>a:active {
background-color: var(--highlight-bgcolor);
color: var(--white);
}
li.page.active>a {
cursor: default;
}
</style>
<ol class="pages">
<c:if test="${pagination.hasPreviousPage}">
<li class="first"><a data-page="1" href="${currentPath}?currentPage=0">&Lang;</a></li>
<c:if test="${pagination.shouldShowFirst and pagination.previousPage gt 0 }">
<li class="prev"><a data-page="${pagination.previousPage+1 }" href="${currentPath}?currentPage=${pagination.previousPage }">&lang;</a></li>
</c:if>
</c:if>
<c:forEach begin="${pagination.startPage}" end="${pagination.endPage }" varStatus="loop">
<c:set var="page" value="${loop.index }" />
<c:choose>
<c:when test="${page eq pagination.currentPage}">
<li class="page active"><a>${page+1}</a></li>
</c:when>
<c:otherwise>
<li class="page"><a href="${currentPath}?currentPage=${page}">${page+1}</a></li>
</c:otherwise>
</c:choose>
</c:forEach>
<c:if test="${pagination.hasNextPage}">
<c:if test="${pagination.shouldShowLast and pagination.nextPage lt pagination.lastPage }">
<li class="next"><a data-page="${pagination.nextPage+1 }" href="${currentPath}?currentPage=${pagination.nextPage }">&rang;</a></li>
</c:if>
<li class="last"><a data-page="${pagination.lastPage+1 }" href="${currentPath}?currentPage=${pagination.lastPage }">&Rang;</a></li>
</c:if>
</ol>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment