Thymeleaf passing 2 parameters to URL

Asked

Viewed 859 times

1

Good evening, everyone,

I have the following problem:

I am trying to pass 2 parameters in the URL using Thymeleaf, but I am not able to recover the value of the variable "name" to pass in the second parameter.

Note: Euconsigo pass the page number.

Please, could you help me?

Controller:

@GetMapping("/listar")
public String listar(@RequestParam(name = "nome", defaultValue = "") String nome,
        @RequestParam(name = "page", defaultValue = "0") int page, ModelMap model) {
    if (!nome.isEmpty()) {
        model.addAttribute("produtos", produtoService.findByNome(PageRequest.of(page, 5), nome));
        model.addAttribute("nome", nome);
    } else {
        model.addAttribute("produtos", produtoService.listarTodos(PageRequest.of(page, 5)));
    }
    model.addAttribute("currentPage", page);
    return "produto/lista";
}

HTML I want to pass the parameters:

<ul class="pagination" style="text-align: center">
    <li class="waves-effect"
        th:each="i: ${#numbers.sequence(0, produtos.totalPages-1)}"><a
        th:href="@{/produtos/listar/(page=${i}, nome=${nome})}"
        th:text="${i}"
        th:classappend="${currentPage}==${i}?'button_actions_back':''"></a></li>
</ul>

1 answer

0


I was able to solve the problem.

The URL assembly was wrong. The correct one would be:

th:href="@{/produtos/listar?page=${i}&nome=${nome}}"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.