Problem to edit an object in spring boot + Thymeleaf?

Asked

Viewed 315 times

-1

This is my controller:

    @GetMapping("/editpost/{id}")
    public ModelAndView editPost(@RequestParam(name="id")long id) {
        Post postFound = nb.findById(id).get();
        ModelAndView mv = new ModelAndView("postEdit");
        mv.addObject("post", postFound);
        return mv;  
    }

This is my view that calls the controller to edit:

<!DOCTYPE html>
<html lang="pt-br" xmlns:th="http://thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Novo Blog Spring</title>
<link rel="stylesheet"
    href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
    integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"
    crossorigin="anonymous">
    <link rel = "stylesheet" type = "text/css" href = "/css/style.css" />
</head>
<body>
    <header th:replace="/fragment1 :: header"></header>
    <section>
        <div class="container mb-5" style="width: 60%;">

            <nav aria-label="breadcrumb">
                <ol class="breadcrumb"  style="background-color: white;" >
                  <li class="breadcrumb-item active mt-5"aria-current="page">Posts</li>
                </ol>
            </nav>
            <th:block  th:each = "post: ${posts}">
            <div class="card mt-2 linebreak">
                <div class="card-body mt-2">
                    <a href=""><h5 th:text="${post.titulo}"></h5></a>
                    <small th:text="${post.autor}"></small>
                    <small th:text="${#dates.format(post.data, 'dd/MM/yyyy')}"></small>
                    <hr>
                    <p class="esconder" th:text="${post.texto}"><p>...</p></p>
                </div>
            </div>
            <a th:href="@{/deletepost/(id=${post.id})}"><button class="btn btn-primary mt-2" style="width:10%">Deletar</button></a>
            <a th:href="@{/editpost/(id=${post.id})}"><button class="btn btn-primary mt-2" style="width:10%">Editar</button></a>
            </th:block>
        </div>

    </section>
</body>
</html>

The problem is that when I click edit, in the url the id shown is correct, but in the browser is displayed the following error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Jan 24 01:56:34 GMT-03:00 2020
There was an unexpected error (type=Not Found, status=404).
No message available

Since postEdit.html exists.

1 answer

0

I found the problem, but I don’t understand why. If I change the

@GetMapping("/editpost/{id}")

for

@GetMapping("/editpost{id}")

it works normally.

Browser other questions tagged

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