controller -> BoardController.java
package com.study.board.controller;
import com.study.board.entity.Board;
import com.study.board.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class BoardController {
@Autowired
private BoardService boardService;
@GetMapping("/board/list")
public String boardList(Model model, @PageableDefault(page = 0, size = 10, sort = "id", direction = Sort.Direction.DESC) Pageable pageable) {
Page<Board> list = boardService.boardList(pageable);
int nowPage = list.getPageable().getPageNumber() + 1;
int firstPage = 1;
int lastPage = list.getTotalPages();
model.addAttribute("list", list);
model.addAttribute("nowPage", nowPage);
model.addAttribute("firstPage", firstPage);
model.addAttribute("lastPage", lastPage);
return "boardlist";
}
}
templates > boardlist.html
<th:block th:each="page : ${#numbers.sequence(firstPage,lastPage)}">
<a th:href="@{/board/list(page=${page - 1})}" th:if="${page != nowPage}" th:text="${page}"></a>
<strong style="color: red" th:if="${page == nowPage}" th:text="${page}"></strong>
</th:block>

