Error trying to update record with Spring MVC

Asked

Viewed 60 times

0

Good afternoon devs... I am studying spring mvc, in my system I have a controler that has a mapped method as @getmappint(/search/{id}), as below:

@GetMapping("buscar/{id}")
public ModelAndView alterarPrestador(@PathVariable ("id") String id, ModelMap model) {
    PrestadorModel user = new PrestadorDao().getPrestador(id);
    model.addAttribute("PrestadorModel", user);
    //model.addAttribute("atualizar", true);
    ModelAndView visao = new ModelAndView("alterar", model);

    return visao;
}

it returns a modelndview, which is the JSP page change. until ai blz, it fills the form fields with the correct information. this and the form:

<body>
<h4>Alterar dados de prestaddor</h4>
<form:form modelAttribute="PrestadorModel" action="update"
    method="post">
    <div>

        <div>
            <form:hidden path="id" />
        </div>

        <div>
            <form:label path="pisPasep">PIS/PASEP:</form:label>
            <form:input path="pisPasep" />
        </div>
        <div>
            <form:label path="nome">Nome completo:</form:label>
            <form:input path="nome" />
        </div>

        <input type="submit" value="Atualizar">

    </div>
</form:form>

when sending the data by this form, to the update() method, it says that the POST method is not supported, this is the method that receives the data from the change.jsp

@PostMapping("update")
public ModelAndView atualizar(@ModelAttribute("PrestadorModel") PrestadorModel prestador, ModelMap model){
    PrestadorDao dao = new PrestadorDao();
    dao.atualizarPrestador(prestador);
    //model.addAttribute("msg", "Dados atualizados com sucesso!!");

    return new ModelAndView("listaprestador");
}

I expect the help of you.

1 answer

0

The solution I found was the following, in my list form.jsp

<h2>Prestadores cadastrados</h2>
<hr>

<div>
    <table>
        <thead>
            <tr>
                <td>CODIGO</td>
                <td>NOME</td>
                <td>PIS/PASEP</td>
            </tr>

        </thead>

        <tbody>

            <c:forEach var="prestador" items="${listaPrestador }">
                <tr>
                    <td>${prestador.id }</td>
                    <td>${prestador.nome }</td>
                    <td>${prestador.pisPasep }</td>
                    <td><a href="buscar?id=${prestador.id }">Alterar</a><img></td>
                    <td><a href="excluir">Excluir</a><img></td>
                </tr>
            </c:forEach>

        </tbody>
    </table>
</div>

in the controler, the method that reads the id in the database:

    @GetMapping("buscar")
public ModelAndView teste(PrestadorModel prestador, ModelMap model) {

    PrestadorDao dao = new PrestadorDao();
    PrestadorModel prest = dao.getPrestador(prestador.getId());
    model.addAttribute("PrestadorModel", prest);
    return new ModelAndView("alterar");
}

The change.jsp page, which will show the recovered data with the given id:

<body>
<h4>Alterar dados de prestaddor</h4>
<form:form modelAttribute="PrestadorModel" action="atualizar"
    method="post">
    <div>

        <div>
            <form:hidden path="id" />
        </div>

        <div>
            <form:label path="pisPasep">PIS/PASEP:</form:label>
            <form:input path="pisPasep" />
        </div>
        <div>
            <form:label path="nome">Nome completo:</form:label>
            <form:input path="nome" />
        </div>

        <input type="submit" value="Atualizar">

    </div>
</form:form>

And in controler, when the user sends from the page change.jsp the data to update:

@PostMapping("atualizar")
public ModelAndView atualizar(@ModelAttribute("PrestadorModel") PrestadorModel prestador, ModelMap model){
    PrestadorDao dao = new PrestadorDao();
    dao.atualizarPrestador(prestador);
    //model.addAttribute("msg", "Dados atualizados com sucesso!!");

    return new ModelAndView("redirect:/prestador/listaprestador");
}

Browser other questions tagged

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