Design using SPRING MVC does not perform CRUD on the bank correctly

Asked

Viewed 47 times

2

I have a Controller with some implemented methods that save, update and remove my object pr of the kind professorRepository on the bench. I am using the Spring boot for this project, then the CRUD is happening through the JPA Repository.

The GET’s and POST’s seem like function correctly, because the application runs and renders on browser the path spent in the requestMapping right. However, I am trying to update and remove my object and it is being saved for all maps, the three forms of path to register, remove and update save the object in the seat.

Anyone have a clue? And (taking advantage), does anyone know any good lessons/documentation about the Jpa Repository in Portuguese?

Follow my code with omitted Imports:

@Controller
@RequestMapping(value = {"/professor.html"})
public class ProfessorController {
    @Autowired
    ProfessorRepository pr;


    @GetMapping(value = "")
    @ResponseBody
    public ModelAndView professorInicio(){
        ModelAndView form = new ModelAndView("/form.html");
        return form;
    }

    @PostMapping(value = "")
    @ResponseBody
    public ModelAndView cadastraProfessor(@RequestParam ("coord") String coord , @RequestParam ("username") String username, @RequestParam ("nome")String nome, @RequestParam("email")String email){
        Professor prof = new Professor();
        ModelAndView index = new ModelAndView("/index.html");
        prof.setUsername(username);
        prof.setNome(nome);
        prof.setEmail(email);
        pr.save(prof);
        return index;
    }

    @GetMapping(value = {"/updateProfessor.html"})
    @ResponseBody
    public ModelAndView professorEdicao(){
        ModelAndView form = new ModelAndView("/form.html");
        return form;
    }

    @PostMapping(value = {"/updateProfessor.html"})
    @ResponseBody
    public ModelAndView editaProfessor(@RequestParam ("username") String username){
        ModelAndView pagSucesso = new ModelAndView("/pagSucesso.html");
        pr.findByUsername(username);
        Professor prof = new Professor();
        prof.setUsername(username);
        pr.save(prof);
        return pagSucesso;
    }

    @GetMapping(value = {"/removeProfessor.html"})
    @ResponseBody
    public ModelAndView professorRemocao(){
        ModelAndView form = new ModelAndView("/form.html");
        return form;
    }

    @PostMapping(value = {"/removeProfessor.html"})
    @ResponseBody
    public ModelAndView removeProfessor(@RequestParam String username){
        ModelAndView pagSucesso = new ModelAndView("/pagSucesso.html");
        pr.findByUsername(username);
        Professor prof = new Professor();
        pr.delete(prof);
        return pagSucesso;
    }
} 
  • 1

    You can open Devtools in Chrome with F12 and in the Network tab see which request is being made by the client.

  • 2

    Thank you! Your tip made me look at Response and I realized that as I was testing with the same view for all methods, this view had an action for the POST only from the register.

  • 1

    You’re welcome, and don’t forget to mark the Post as solved.

  • 1

    @Isaíasdelimacoelho, here in Stack Overflow we do not mark as solved. We write a reply and mark it as the accepted answer.

No answers

Browser other questions tagged

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