Created
August 3, 2016 10:28
-
-
Save dotkebi/fe7725259bb0a7a2714b22739babcd80 to your computer and use it in GitHub Desktop.
Velocity paging snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul> | |
#if (${pageInfo.pageablePrevious}) | |
<li><a href="${pageInfo.link}?page=0">«</a></li> | |
#if (${pageInfo.previous} != -1) | |
<li><a href="${pageInfo.link}?page=$pageInfo.previous">‹</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">›</a></li> | |
#if (${pageInfo.next} != -1) | |
<li><a href="${pageInfo.link}?page=$pageInfo.totalPages">»</a></li> | |
#end | |
#end | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment