0

I don´t know how redirect after post in my modelAndView

@PostMapping("/admin/nuevo_equipo")
public ModelAndView createEntrenador(@Valid Equipo equipo, BindingResult bindingResult) {
    ModelAndView model = new ModelAndView();
    equipoService.save(equipo);
    model.addObject("msg", "entrenador creado con exito");
    model.setViewName("/administrador/nuevo_equipo");
    return model;
}

How can I do it?

2 Answers2

2

Normal redirect like below.

@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public ModelAndView method() {
    return new ModelAndView("redirect:" + url);
}

Send data with redirection (flash attribute data will not be visible in URL):

@RequestMapping(value="/someURL", method=GET)
public String yourMethod(RedirectAttributes redirectAttributes)
{
   ...
   redirectAttributes.addAttribute("rd", "rdValue");
   redirectAttributes.addFlashAttribute("fa", faValue);
   return "redirect:/someOtherURL";
}

As i can see from the code that you are trying to send data also along with redirection so use 2nd approach.

Also see this spring-redirectattributes-addattribute-vs-addflashattribute

Alien
  • 15,141
  • 6
  • 37
  • 57
0

there are several possibilities... in your case following would be a good fit

@PostMapping("/admin/nuevo_equipo")
public ModelAndView createEntrenador(@Valid Equipo equipo, BindingResult bindingResult, ModelMap model) {
    equipoService.save(equipo);
    model.addAttribute("msg", "entrenador creado con exito");
    return new ModelAndView("redirect:/REDIRECT_URL", model);
}

Important to know is, that your redirect is a redirect to a new URL and your not directly responding with a new view.

lhaidacher
  • 161
  • 4