I'm using spring boot and thymeleaf.
I have a page with a form that search by a specific param then submits the data as POST request.
The page shows the result in a table and works fine. The table shows the correct pagination options and result,
Problem: When I click the button to show results on page 2 o laters, then the POST data from the previous search is lost.
Question: How can I retain the POSTed data during pagination?
CONTROLLER:
@RequestMapping(value = "/list",method = {RequestMethod.POST, RequestMethod.GET})
public String list(@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "name", defaultValue = "") String name,
@RequestParam(name = "lastname", defaultValue = "") String lastname,
Pageable pageable,
Model model) {
Pageable pageRequest = new PageRequest(page, 4);
Cliente cliente = new Cliente();
Page<Cliente> clientes;
if (name.equals("") && lastname.equals("")) {
clientes = clienteService.findAll(pageRequest);
}else {
clientes = clienteService.findMatch(name, lastname, pageRequest);
}
PageRender<Cliente> pageRender = new PageRender<Cliente>("/listar", clientes);
model.addAttribute("titulo", "Listado de clientes");
model.addAttribute("clientes", clientes);
model.addAttribute("cliente", cliente);
model.addAttribute("page", pageRender);
return "listar";
}
HTML:
<form th:action="@{/listar}" th:object="${cliente}" method="post">
<div class="form-group row">
<label for="nombre" class="col-sm-2 col-form-label">Nombre</label>
<div class="col-sm-6">
<input type="text" th:field="*{nombre}" class="form-control"
th:errorclass="'form-control alert-danger'" /> <small
class="form-text text-danger"
th:if="${#fields.hasErrors('nombre')}" th:errors="*{nombre}"></small>
</div>
</div>
<div class="form-group row">
<label for="nombre" class="col-sm-2 col-form-label">Apellido</label>
<div class="col-sm-6">
<input type="text" th:field="*{apellido}" class="form-control"
th:errorclass="'form-control alert-danger'" /> <small
class="form-text text-danger"
th:if="${#fields.hasErrors('apellido')}" th:errors="*{apellido}"></small>
</div>
</div>
<div class="form-group row">
<div class="col-sm-6">
<input type="submit" th:value="'Buscar'"
class="btn btn-secondary" />
</div>
</div>
</form>
Pagination:
<nav th:fragment="paginator">
<ul class="pagination">
<li class="page-item"
th:class="${page.first? 'page-item disabled': 'page-item'}"><span
class="page-link" th:if="${page.first}">Primera</span> <a
class="page-link" th:if="${not page.first}"
th:href="@{${page.url}(page=0)}">Primera</a></li>
<li class="page-item"
th:class="${not page.hasPrevious? 'page-item disabled': 'page-item'}">
<span class="page-link" th:if="${not page.hasPrevious}">«</span>
<a class="page-link" th:if="${page.hasPrevious}"
th:href="@{${page.url}(page=${page.paginaActual-2})}">«</a>
</li>
<li class="page-item" th:each="item : ${page.paginas}"
th:class="${item.actual? 'page-item active': 'page-item'}"><span
class="page-link" th:if="${item.actual}" th:text="${item.numero}"></span>
<a class="page-link" th:if="${not item.actual}"
th:href="@{${page.url}(page=${item.numero-1})}"
th:text="${item.numero}"></a></li>
<li class="page-item"
th:class="${not page.hasNext? 'page-item disabled': 'page-item'}">
<span class="page-link" th:if="${not page.hasNext}">»</span> <a
class="page-link" th:if="${page.hasNext}"
th:href="@{${page.url}(page=${page.paginaActual})}">»</a>
</li>
<li class="page-item"
th:class="${page.last? 'page-item disabled': 'page-item'}"><span
class="page-link" th:if="${page.last}">Última</span> <a
class="page-link" th:if="${not page.last}"
th:href="@{${page.url}(page=${page.totalPaginas-1})}">Última</a>
</li>
</ul>
</nav>