0
votes

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}">&laquo;</span>
            <a class="page-link" th:if="${page.hasPrevious}"
            th:href="@{${page.url}(page=${page.paginaActual-2})}">&laquo;</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}">&raquo;</span> <a
            class="page-link" th:if="${page.hasNext}"
            th:href="@{${page.url}(page=${page.paginaActual})}">&raquo;</a>
        </li>
        <li class="page-item"
            th:class="${page.last? 'page-item disabled': 'page-item'}"><span
            class="page-link" th:if="${page.last}">&Uacute;ltima</span> <a
            class="page-link" th:if="${not page.last}"
            th:href="@{${page.url}(page=${page.totalPaginas-1})}">&Uacute;ltima</a>
        </li>
    </ul>

</nav>
1
Please provide your form code in question. Note that HTTP is stateless meaning every request is a totally new one to the server (almost all times).Mahozad
Done, I add the HTML (thymeleaf) FORM and the Pagination.Lubelmont
@Lubelmont, Did you solve this problem?pyOwner

1 Answers

0
votes

I had a similar situation where I used the request parameter. So the trick is to capture the request parameter based on what we perform the search. Then afterwards you can get that parameter (from URL) and use it in the links you are building for pagination.

For example, say, your post/get URL looks like this:

http://localhost:8080/listByName?name=earth

Then get the vale of "name" parameter and use it while building your pagination url (links) in your html table/list like this:

th:href="@{/listByName/(name=${#request.getParameter('name')},pageSize=${selectedPageSize}, page=${page})}"

for me it working perfectly. This way you wont have to go for additional code using flashAttributes or mucking with JavaScript.