기본 콘텐츠로 건너뛰기

JPA Pagenation(Pageable)

JpaRepository
public interface FreeboardRepository extends JpaRepository<Freeboard, Integer> {
    public Page<Freeboard> findByBname(String bname, Pageable pageable);
    public Page<Freeboard> findAll(Pageable pageable);
}

Controller
@GetMapping({"","/"})
    public String freeboard(Model m, String bname, @PageableDefault(size=3, sort="fbnum"/*, direction = Sort.Direction.DESC */, page=0) Pageable pageable) {
    Page<Freeboard> pageFreeboard = freeboardService.getListByBname(bname!=null?bname:"free",pageable);
    m.addAttribute("pageFreeboard", pageFreeboard);
    m.addAttribute("bname",bname);
    return "html/freeboard/freeBoard";
}

.html
<div>
    <span th:if="${pageFreeboard.getNumber() gt 9}"><a th:href="@{/freeboard(page=${pageFreeboard.getNumber()/10*10-1})}">이전</a></span>
    <span th:if="${i le pageFreeboard.getTotalPages()}" th:each="i : ${#numbers.sequence(pageFreeboard.getNumber()/10*10+1,pageFreeboard.getNumber()/10*10+10)}">
        <a th:if="${i!=pageFreeboard.getNumber()+1}" th:href="@{/freeboard(page=${i-1})}">[[${i}]]</a>
        <span th:if="${i==pageFreeboard.getNumber()+1}" th:href="@{/freeboard(page=${i-1})}">[[${i}]]</span>
    </span>
    <span th:if="${(pageFreeboard.getNumber()/10*10+10)<pageFreeboard.getTotalPages()}"><a th:href="@{/freeboard(page=${pageFreeboard.getNumber()/10*10+10})}">다음</a></span>
</div>

이 블로그의 인기 게시물