Skip to content

Instantly share code, notes, and snippets.

@dotkebi
Created August 3, 2016 10:28
Show Gist options
  • Save dotkebi/fe7725259bb0a7a2714b22739babcd80 to your computer and use it in GitHub Desktop.
Save dotkebi/fe7725259bb0a7a2714b22739babcd80 to your computer and use it in GitHub Desktop.
Velocity paging snippet
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author by [email protected] on 2016-08-01.
*/
@Data
@NoArgsConstructor
public class PageInfo {
public static final int QUANTITY_OF_CONTENTS_PER_PAGE = 20;
public static final int QUANTITY_OF_PAGES = 10;
/**
* 현재 페이지
*/
private int page;
/**
* 페이지 이동
*/
private boolean pageablePrevious;
private boolean pageableNext = true;
private int startPage;
private int maxPage;
private int previous;
private int next;
/**
* 게시물 시작 위치
*/
private int offset;
/**
* 한번에 가져올 게시물 숫자
*/
private int limit = QUANTITY_OF_CONTENTS_PER_PAGE;
/**
* 전체 게시물 숫자
*/
private int total;
/**
* 전체 게시물 숫자
*/
private int totalPages;
/**
* 연결할 링크
*/
private String link;
/**
* 검색어
*/
private String subject = "";
private String keyword = "";
public void setUp(String link, Integer total) {
this.link = link;
this.total = total;
if (page == 0) {
page = 1;
}
offset = (page - 1) * limit;
totalPages = total / limit + ((total % limit > 0) ? 1 : 0);
int remainder = page % QUANTITY_OF_PAGES;
if (remainder == 0) {
remainder = QUANTITY_OF_PAGES;
}
startPage = page - remainder + 1;
maxPage = startPage + QUANTITY_OF_PAGES - 1;
if (maxPage > totalPages) {
maxPage = totalPages;
pageableNext = false;
} else {
pageableNext = true;
}
pageablePrevious = startPage / QUANTITY_OF_PAGES > 0;
previous = page - 1;
if (previous < 1) {
previous = -1;
}
next = maxPage + 1;
if (next > totalPages) {
next = -1;
}
if (this.offset < 0) {
this.offset = 0;
}
}
}
<ul>
#if (${pageInfo.pageablePrevious})
<li><a href="${pageInfo.link}?page=0">&laquo;</a></li>
#if (${pageInfo.previous} != -1)
<li><a href="${pageInfo.link}?page=$pageInfo.previous">&lsaquo;</a></li>
#end
#end
#foreach($i in [${pageInfo.startPage}..${pageInfo.maxPage}])
<li #if(${pageInfo.page} == $i) class="active" #end>
<a href="${pageInfo.link}?page=$i">$i</a>
</li>
#end
#if (${pageInfo.pageableNext})
<li><a href="${pageInfo.link}?page=$pageInfo.next">&rsaquo;</a></li>
#if (${pageInfo.next} != -1)
<li><a href="${pageInfo.link}?page=$pageInfo.totalPages">&raquo;</a></li>
#end
#end
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment