Langue: java 1.8 Framework: spring boot 2.2.4.RELEASE (spring-boot-starter) Moteur de modèle: thymeleaf 2.2.4.RELEASE (spring-boot-starter) Base de données: H2 1.4.200 (démarreur à ressort) 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 omis
    
}
Pagination
public class Pagination {
	/** 1.Nombre de bulletins à afficher sur la page**/
	private int pageSize = 10;
	/** 2.Nombre de blocs paginés**/
	private int blockSize = 10;
	/** 3.Page actuelle**/
	private int page = 1;
	/** 4.Actuellement bloqué**/
	private int block = 1;
	/** 5.Nombre total de bulletins**/
	private int totalListCnt;
	/** 6.Nombre total de pages**/
	private int totalPageCnt;
	/** 7.Nombre total de blocs**/
	private int totalBlockCnt;
	/** 8.Bloquer la page de démarrage**/
	private int startPage = 1;
	/** 9.Dernière page du bloc**/
	private int endPage = 1;
	/** 10.Index de démarrage de l'approche DB**/
	private int startIndex = 0;
	/** 11.Dernière page du bloc précédent**/
	private int prevBlock;
	/** 12.Dernière page du bloc suivant**/
	private int nextBlock;
    
    //Getter / Setter omis
	public Pagination(int totalListCnt, int page) {
		//Obtenez le nombre total de messages et la page actuelle du contrôleur.
		//Nombre total de bulletins- totalListCnt
		//Page actuelle- page
		
		/** 3.Page actuelle**/
		setPage(page);
		
		/** 5.Nombre total de bulletins**/
		setTotalListCnt(totalListCnt);
		/** 6.Nombre total de pages**/
		setTotalPageCnt((int) Math.ceil(totalListCnt * 1.0 / pageSize));
		/** 7.Nombre total de blocs**/
		setTotalBlockCnt((int) Math.ceil(totalPageCnt * 1.0 / blockSize));
		
		/** 4.Actuellement bloqué**/
		setBlock((int) Math.ceil((page * 1.0)/blockSize)); 
		/** 8.Bloquer la page de démarrage**/
		setStartPage((block - 1) * blockSize + 1);
		
		/** 9.Dernière page du bloc**/
		setEndPage(startPage + blockSize - 1);
        
		/* ===Variations sur la dernière page noire===*/
		if(endPage > totalPageCnt){this.endPage = totalPageCnt;}
		
		/** 11.Dernière page du bloc précédent**/
		setPrevBlock((block * blockSize) - blockSize);
		/* ===Variations sur les blocs précédents=== */
		if(prevBlock < 1) {this.prevBlock = 1;}
		/** 12.Dernière page du bloc suivant**/
		setNextBlock((block * blockSize) + 1);
		
		/* ===Variations sur le bloc suivant===*/
		if(nextBlock > totalPageCnt) {nextBlock = totalPageCnt;}
        
		/** 10.Index de démarrage de l'approche DB**/
		setStartIndex((page-1) * pageSize);
	
	}
}
Controller
@GetMapping("/")
public String home(Model model, @RequestParam(defaultValue = "1") int page) {
    //Nombre total de bulletins
    int totalListCnt = boardRepository.findAllCnt();
    //Nombre total d'articles et page actuelle
    Pagination pagination = new Pagination(totalListCnt, page);
    //Index de démarrage de l'approche DB
    int startIndex = pagination.getStartIndex();
    
    //Nombre maximum de bulletins à afficher sur une page
    int pageSize = pagination.getPageSize();
	//Acquisition de bulletin
    List<Board> boardList = boardRepository.findListPaging(startIndex, pageSize);
	//Stocker l'objet dans l'objet modèle
    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>
//pagination
<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