[JAVA] Implémenter la pagination à l'aide de SpringData Pageable + Thymeleaf

Objectif

・ Je veux bien paginer l'écran de la liste ・ La mise en œuvre doit être aussi simple que possible

environnement

SpringBoot 2.1.0.RELEASE Spring Data Commons 2.1.2.RELEASE https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Pageable.html

la mise en oeuvre

Configuration


import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyAppWebConfig implements WebMvcConfigurer {

    @Override
    public void addArgumentResolvers(
            List<HandlerMethodArgumentResolver> argumentResolvers) {
        PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
        //Le nombre maximum d'éléments affichés sur une page est augmenté à 10
        resolver.setMaxPageSize(10);
        argumentResolvers.add(resolver);
    }
}

Repository J'ai utilisé JPA.


import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface MyHistoryRepository
        extends PagingAndSortingRepository<MyHistory, Long> {

    Page<MyHistory> findByUserIdAndDeletedFalse(
            String userId, Pageable pageable);

}

Service


@Service
@Transactional(readOnly = true)
public class MyHistoryServiceImpl implements MyHistoryService {

    @Autowired
    MyHistoryRepository myHistoryRepository;

    public Page<MyHistoryModel> getMyHistoryByUserId(
            String userId, Pageable pageable) {
        //Convertir l'entité acquise en modèle et la renvoyer
        return myHistoryRepository
                .findByUserIdAndDeletedFalse(userId,
                        pageable)
                .map(MyHistoryConverter::entityToModel);
    }
}

Controller


@Controller
@RequestMapping("/mypage")
public class MypageController {

    @Autowired
    MyHistoryService myHistoryService;

    @GetMapping
    public String doGetMypage(@AuthenticationPrincipal MyUserDetails user,
            Model model, Pageable pageable) {
        model.addAttribute("myHistory", myHistoryService
                .getMyHistoryByUserId(user.getUserId(), pageable));
        return "view";
    }
}

View(Thymeleaf)

<!--Seulement au téléavertisseur-->
<div th:unless="${myHistory.getContent().size()==0}" th:fragment='paginationbar'>
		<ul>
			<li th:class="${myHistory.first} ? 'disabled':''" style="display:inline">
				<span th:if="${myHistory.first}">first</span>
				<a th:if="${not myHistory.first}" th:href="@{${'/mypage'}(page=0)}">first</a>
			</li>
			<li th:each='i : ${#numbers.sequence(0, myHistory.totalPages-1)}' th:class="(${i}==${myHistory.number})? 'active' : ''" style="display:inline">
		<span th:if='${i}==${myHistory.number}' th:text='${i+1}'>1</span>
			<a th:if='${i}!=${myHistory.number}' th:href="@{${'/mypage'}(page=${i})}">
				<span th:text='${i+1}'>1</span>
			</a>
		</li>
		<li th:class="${myHistory.last} ? 'disabled':''" style="display:inline">
			<span th:if="${myHistory.last}">last</span>
			<a th:if="${not myHistory.last}" th:href="@{${'/mypage'}(page=(${myHistory.totalPages}-1))}">last</a>
		</li>
	</ul>
</div>

Impressions

J'ai pu facilement implémenter la pagination en passant Pageable. Cette fois, l'entité acquise elle-même a été ciblée pour la nation de la page. Dans cette implémentation, si vous pouvez paginer sur un champ de l'entité acquise, J'ai été capable de le gérer avec presque aucune modification du code source existant, mais d'après ce que j'ai étudié, cela me semblait impossible.

Recommended Posts

Implémenter la pagination à l'aide de SpringData Pageable + Thymeleaf
Implémenter la pagination des rails