| 3 | import org.springframework.web.util.*; |
| 4 | |
| 5 | public class PageHandler { |
| 6 | private SearchCondition sc; |
| 7 | // private int pageSize = 10; // 한 페이지당 게시물 갯수 |
| 8 | // private int page; // 현재 페이지 |
| 9 | // private String option; |
| 10 | // private String keyword; |
| 11 | public final int NAV_SIZE = 10; // page navigation size |
| 12 | private int totalCnt; // 게시물의 총 갯수 |
| 13 | private int totalPage; // 전체 페이지의 갯수 |
| 14 | private int beginPage; // 화면에 보여줄 첫 페이지 |
| 15 | private int endPage; // 화면에 보여줄 마지막 페이지 |
| 16 | private boolean showNext = false; // 이후를 보여줄지의 여부. endPage==totalPage이면, showNext는 false |
| 17 | private boolean showPrev = false; // 이전을 보여줄지의 여부. beginPage==1이 아니면 showPrev는 false |
| 18 | |
| 19 | public PageHandler(int totalCnt, Integer page) { |
| 20 | this(totalCnt, new SearchCondition(page, 10)); |
| 21 | } |
| 22 | |
| 23 | public PageHandler(int totalCnt, Integer page, Integer pageSize) { |
| 24 | this(totalCnt, new SearchCondition(page, pageSize)); |
| 25 | } |
| 26 | |
| 27 | public PageHandler(int totalCnt, SearchCondition sc) { |
| 28 | this.totalCnt = totalCnt; |
| 29 | this.sc = sc; |
| 30 | |
| 31 | doPaging(totalCnt, sc); |
| 32 | } |
| 33 | |
| 34 | private void doPaging(int totalCnt, SearchCondition sc) { |
| 35 | this.totalPage = totalCnt / sc.getPageSize() + (totalCnt % sc.getPageSize()==0? 0:1); |
| 36 | this.sc.setPage(Math.min(sc.getPage(), totalPage)); // page가 totalPage보다 크지 않게 |
| 37 | this.beginPage = (this.sc.getPage() -1) / NAV_SIZE * NAV_SIZE + 1; // 11 -> 11, 10 -> 1, 15->11. 따로 떼어내서 테스트 |
| 38 | this.endPage = Math.min(beginPage + NAV_SIZE - 1, totalPage); |
| 39 | this.showPrev = beginPage!=1; |
| 40 | this.showNext = endPage!=totalPage; |
| 41 | } |
| 42 | |
| 43 | public String getQueryString() { |
| 44 | return getQueryString(this.sc.getPage()); |
| 45 | } |
| 46 | |
| 47 | public String getQueryString(Integer page) { |
| 48 | // ?page=10&pageSize=10&option=A&keyword=title |
| 49 | return UriComponentsBuilder.newInstance() |
| 50 | .queryParam("page", page) |
| 51 | .queryParam("pageSize", sc.getPageSize()) |
| 52 | .queryParam("option", sc.getOption()) |
| 53 | .queryParam("keyword", sc.getKeyword()) |
| 54 | .build().toString(); |
| 55 | } |
| 56 | |
| 57 | void print() { |
| 58 | System.out.println("page="+ sc.getPage()); |
| 59 | System.out.print(showPrev? "PREV " : ""); |
| 60 | |
| 61 | for(int i=beginPage;i<=endPage;i++) { |
| 62 | System.out.print(i+" "); |
nothing calls this directly
no outgoing calls
no test coverage detected