'filter while pagination with spring mvc and thymeleaf

I'm trying to paginate while searching in thymeleaf, I don't know if anyone has a way to do it or it's better to use jquery for pagination, At the moment I am using two different requests, one is GET to list and the other is POST to filter it.

sorry for my english ,it's not my mother tongue.

My controller

@GetMapping("/listado-clientes")
public String listadoClientes(@RequestParam("page") int numPagina,Model model) {
    
    Page<Cliente> clientesPage = clienteService.listarClientes(numPagina);
    List<Cliente> clientes = clientesPage.getContent();

    model.addAttribute("clientes",clientes);
    model.addAttribute("numberPages",clientesPage.getTotalPages());
    model.addAttribute("currentPage",numPagina);
    
    return "/cliente/listado-clientes";

}

@PostMapping("/listado-clientes")
public String listadoClientes(@RequestParam("page") int numPagina,@RequestParam("search") String searchString,Model model) {
    
    System.out.println("test");

    Page<Cliente> clientesPage = clienteService.listarPorDni(numPagina,searchString);
    List<Cliente> clientes = clientesPage.getContent();

    model.addAttribute("clientes",clientes);
    model.addAttribute("numberPages",clientesPage.getTotalPages());
    model.addAttribute("currentPage",numPagina);

    return "/cliente/listado-clientes :: client-list-table";
}

My service

@Override
@Transactional
public Page<Cliente> listarPorDni(int pagina,String searchString) {
    Pageable pagination = PageRequest.of(pagina - 1, pageSize);      
    
    try {
        log.info("Devolviendo lista de clientes...");
        if(searchString.isEmpty()) {
            System.out.println("test");
            return clienteRepository.findAll(pagination);
        } else {
            System.out.println("test2");
            return clienteRepository.findAllByDni(pagination, searchString);
        }
                
    } catch (Exception ex) {
        log.error("Un error ha ocurrido mientras se realiza la operacion: " + ex.getMessage());
        ex.printStackTrace();
        
        return null;
    }
    
}

@Override
@Transactional
public Page<Cliente> listarClientes(int pagina) {       
    
    try {
        Pageable pagination = PageRequest.of(pagina - 1, pageSize);
        log.info("Devolviendo lista de clientes...");
    
        return clienteRepository.findAll(pagination);
        
    } catch (Exception ex) {
        log.error("Un error ha ocurrido mientras se realiza la operacion: " + ex.getMessage());
        ex.printStackTrace();
        
        return null;
    }
    
}

DAO

@Repository
public interface ClienteRepository extends JpaRepository<Cliente, Integer> {
    
    @Query("FROM Cliente t where t.dni like CONCAT('%',:dni,'%')") 
    Page<Cliente> findAllByDni(Pageable pageable , @Param("dni") String dni);
    
    boolean existsByDni(String dni);

}

thymeleaf

 <nav aria-label="Page navigation">
                            <ul class="pagination justify-content-end">
                                <li class="page-item">
                                    <th:block th:if="${currentPage} == 1">                              
                                        <a class="page-link pagination">Previous</a>
                                    </th:block> 
                                    
                                    <th:block th:unless="${currentPage} == 1">
                                        <a class="page-link"
                                            th:href="${'/cliente/listado-clientes?page=' + {currentPage - 1}}">Previous</a>
                                    </th:block>
                                </li>
                                    
                                <th:block th:if="${numberPages} == 0">
                                    <li class="page-item">
                                    <a class="page-link selected" th:href="${'/cliente/listado-clientes?page=1'}" th:text="1">
                                    </a>
                                    </li>
                                </th:block> 
                                
                                <th:block th:unless="${numberPages} == 0">
                                
                                    <th:block th:each="i : ${#numbers.sequence( 1, {numberPages})}">
                                        <li class="page-item">
                                            <a class="page-link" th:classappend="${currentPage == i}? 'selected':''"
                                            th:href="${'/cliente/listado-clientes?page=' + {i}}"
                                            th:text="${i}">
                                            </a>
                                        </li>
                                    </th:block>
                                
                                </th:block>
    
                                <li class="page-item">
                                <th:block
                                        th:if="${currentPage == numberPages}">
                                        <a class="page-link pagination">Next</a>
                                </th:block> 
                                <th:block th:unless="${currentPage == numberPages}">
                                        <a class="page-link"
                                            th:href="${'/cliente/listado-clientes?page=' + {currentPage + 1}}">Next</a>
                                </th:block>
                                </li>
                            </ul>
                        </nav>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source