Language: java 1.8 Framework: spring boot 2.2.4.RELEASE (spring-boot-starter) Template engine: thymeleaf 2.2.4.RELEASE (spring-boot-starter) Database: H2 1.4.200 (spring-boot-starter) orm : data-jpa 2.2.4.RELEASE (spring-boot-starter)
Entity
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Board {
@Id @GeneratedValue
private String id;
private String title;
private String user;
//Getter / Setter omitted
}
Pagination
public class Pagination {
/** 1.Number of bulletins to be displayed on the page**/
private int pageSize = 10;
/** 2.Number of paging blocks**/
private int blockSize = 10;
/** 3.Current page**/
private int page = 1;
/** 4.Currently blocked**/
private int block = 1;
/** 5.Total number of bulletins**/
private int totalListCnt;
/** 6.Total number of pages**/
private int totalPageCnt;
/** 7.Total number of blocks**/
private int totalBlockCnt;
/** 8.Block start page**/
private int startPage = 1;
/** 9.Last page of block**/
private int endPage = 1;
/** 10.DB approach start index**/
private int startIndex = 0;
/** 11.Last page of previous block**/
private int prevBlock;
/** 12.Last page of next block**/
private int nextBlock;
//Getter / Setter omitted
public Pagination(int totalListCnt, int page) {
//Get the total number of posts and the current page from the controller.
//Total number of bulletins- totalListCnt
//Current page- page
/** 3.Current page**/
setPage(page);
/** 5.Total number of bulletins**/
setTotalListCnt(totalListCnt);
/** 6.Total number of pages**/
setTotalPageCnt((int) Math.ceil(totalListCnt * 1.0 / pageSize));
/** 7.Total number of blocks**/
setTotalBlockCnt((int) Math.ceil(totalPageCnt * 1.0 / blockSize));
/** 4.Currently blocked**/
setBlock((int) Math.ceil((page * 1.0)/blockSize));
/** 8.Block start page**/
setStartPage((block - 1) * blockSize + 1);
/** 9.Last page of block**/
setEndPage(startPage + blockSize - 1);
/* ===Variations on the last black page===*/
if(endPage > totalPageCnt){this.endPage = totalPageCnt;}
/** 11.Last page of previous block**/
setPrevBlock((block * blockSize) - blockSize);
/* ===Variations on previous blocks=== */
if(prevBlock < 1) {this.prevBlock = 1;}
/** 12.Last page of next block**/
setNextBlock((block * blockSize) + 1);
/* ===Variations on the next block===*/
if(nextBlock > totalPageCnt) {nextBlock = totalPageCnt;}
/** 10.DB approach start index**/
setStartIndex((page-1) * pageSize);
}
}
Controller
@GetMapping("/")
public String home(Model model, @RequestParam(defaultValue = "1") int page) {
//Total number of bulletins
int totalListCnt = boardRepository.findAllCnt();
//Total number of posts and current page
Pagination pagination = new Pagination(totalListCnt, page);
//DB approach start index
int startIndex = pagination.getStartIndex();
//Maximum number of bulletins to display on a page
int pageSize = pagination.getPageSize();
//Bulletin acquisition
List<Board> boardList = boardRepository.findListPaging(startIndex, pageSize);
//Object storage in model object
model.addAttribute("boardList", boardList);
model.addAttribute("pagination", pagination);
return "index";
}
Repository
@Repository
public class BoardRepository {
@PersistenceContext
private EntityManager em;
public int findAllCnt() {
return ((Number) em.createQuery("select count(*) from Board")
.getSingleResult()).intValue();
}
public List<Board> findListPaging(int startIndex, int pageSize) {
return em.createQuery("select b from Board b", Board.class)
.setFirstResult(startIndex)
.setMaxResults(pageSize)
.getResultList();
}
}
html
<!DOCTYPE html>
<html lang="ja"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>paging</title>
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}">
</head>
<body>
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" style="width: 10%">no</th>
<th scope="col">title</th>
<th scope="col" style="width: 15%">user</th>
</tr>
</thead>
<tbody>
<tr th:each="board : ${boardList}">
<th scope="row" th:text="${boardStat.index + 1}">1</th>
<td th:text="${board.title}"></td>
<td th:text="${board.user}"></td>
</tr>
</tbody>
</table>
//paging
<nav aria-label="Page navigation example ">
<ul class="pagination">
<li class="page-item">
<a class="page-link" th:href="@{/?page=1}" aria-label="Previous">
<span aria-hidden="true"><<</span>
</a>
</li>
<li class="page-item">
<a class="page-link" th:href="@{/?page={page} (page = ${pagination.prevBlock})}" aria-label="Previous">
<span aria-hidden="true"><</span>
</a>
</li>
<th:block th:with="start = ${pagination.startPage}, end = ${pagination.endPage}">
<li class="page-item"
th:with="start = ${pagination.startPage}, end = ${pagination.endPage}"
th:each="pageButton : ${#numbers.sequence(start, end)}">
<a class="page-link" th:href="@{/?page={page} (page = ${pageButton})}" th:text=${pageButton}></a>
</li>
</th:block>
<li class="page-item">
<a class="page-link" th:href="@{?page={page} (page = ${pagination.nextBlock})}" aria-label="Next">
<span aria-hidden="true">></span>
</a>
</li>
<li class="page-item">
<a class="page-link" th:href="@{?page={page} (page = ${pagination.totalPageCnt})}" aria-label="Previous">
<span aria-hidden="true">>></span>
</a>
</li>
</ul>
</nav>
</body>
</html>
Recommended Posts