Skip to content

Instantly share code, notes, and snippets.

@laughingman7743
Created February 25, 2015 00:34
Show Gist options
  • Save laughingman7743/5a33fbf05c305a74b71b to your computer and use it in GitHub Desktop.
Save laughingman7743/5a33fbf05c305a74b71b to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="ja"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title></title>
<link rel="stylesheet" href="../../resources/bower_components/bootstrap/dist/css/bootstrap.min.css" th:href="@{/bower_components/bootstrap/dist/css/bootstrap.min.css}"/>
</head>
<body>
<div class="container">
<nav class="clearfix" th:fragment="pagination">
<ul class="pagination">
<li th:class="${!pagination.hasPrev()} ? disabled">
<a href="javascript:void(0);" aria-label="Previous" th:href="${pagination.hasPrev()} ? ${pagination.getUrlForOtherPage(#httpServletRequest, pagination.getPage() - 1)} : 'javascript:void(0);'">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li th:each="page : ${pagination.getPages()}" th:class="${page == pagination.getPage()} ? 'active' : (${page == -1} ? 'disabled')">
<a href="javascript:void(0);" th:if="${page != -1}" th:text="${page}" th:href="${page != pagination.getPage()} ? ${pagination.getUrlForOtherPage(#httpServletRequest, page)} : 'javascript:void(0);'">1</a>
<a href="javascript:void(0);" th:unless="${page != -1}" th:text="...">...</a>
</li>
<li th:class="${!pagination.hasNext()} ? disabled">
<a href="javascript:void(0)" aria-label="Next" th:href="${pagination.hasNext()} ? ${pagination.getUrlForOtherPage(#httpServletRequest, pagination.getPage() + 1)} : 'javascript:void(0);'">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</ul>
</nav>
</div>
<script src="../../resources/bower_components/jquery/dist/jquery.min.js" th:src="@{/bower_components/jquery/dist/jquery.min.js}"></script>
<script src="../../resources/bower_components/bootstrap/dist/js/bootstrap.min.js" th:src="@{/bower_components/bootstrap/dist/js/bootstrap.min.js}"></script>
</body>
</html>
package com.github.laughingman7743.util;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class SimplePagination {
public static final String DEFAULT_ENCODING = "UTF-8";
public static final String URL_PARAM_PAGE = "page";
public static final String URL_PARAM_PERPAGE = "max";
public static final int LEFT_EDGE = 2;
public static final int RIGHT_EDGE = 2;
public static final int LEFT_CURRENT = 2;
public static final int RIGHT_CURRENT = 3;
private final long page;
private final long perPage;
private final long totalCount;
public SimplePagination(long page, long perPage, long totalCount) {
this.page = page;
this.perPage = perPage;
this.totalCount = totalCount;
}
public long getPage() {
return this.page;
}
public long getPerPage() {
return this.perPage;
}
public long getTotalCount() {
return this.totalCount;
}
public long getTotalPage() {
return (long) Math.ceil(this.totalCount / (double) this.perPage);
}
public boolean hasPrev() {
return this.page > 1;
}
public boolean hasNext() {
return this.page < getTotalPage();
}
public long getFirstPage() {
return this.totalCount == 0 ? 0 : ((this.page - 1) * this.perPage) + 1;
}
public long getLastPage() {
long last = this.page * this.perPage;
return last < this.totalCount ? last : this.totalCount;
}
public List<Long> getPages() {
return getPages(LEFT_EDGE, LEFT_CURRENT, RIGHT_CURRENT, RIGHT_EDGE);
}
public List<Long> getPages(int leftEdge, int leftCurrent, int rightCurrent, int rightEdge) {
long last = 0;
long pages = getTotalPage();
List<Long> result = new LinkedList<>();
for(long i = 1; i <= pages; i ++) {
if((i <= leftEdge) ||
((i > (this.page - leftCurrent - 1)) && (i < (this.page + rightCurrent))) ||
(i > pages - rightEdge)) {
if(last + 1 != i) {
result.add(-1L);
}
result.add(i);
last = i;
}
}
return result;
}
public String getUrlForOtherPage(HttpServletRequest request, long page)
throws UnsupportedEncodingException {
return getUrlForOtherPage(request, page, DEFAULT_ENCODING);
}
public String getUrlForOtherPage(HttpServletRequest request, long page, String encoding)
throws UnsupportedEncodingException{
Map<String, String[]> params = request.getParameterMap();
StringBuilder builder = new StringBuilder();
builder.append(request.getRequestURI());
builder.append("?");
for(Map.Entry<String, String[]> entry : params.entrySet()) {
String key = entry.getKey();
for(String value : entry.getValue()) {
if(!StringUtils.equals(URL_PARAM_PAGE, key)) {
builder.append(URLEncoder.encode(key, encoding));
builder.append("=");
builder.append(URLEncoder.encode(value, encoding));
builder.append("&");
}
}
}
builder.append(URL_PARAM_PAGE);
builder.append("=");
builder.append(page);
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment