Springboot passing ModelAndView from a controller to another

Viewed 12

In Springboot 2.7 I have a simple controller that dinamically sets up a page using a static resource:

@RequestMapping(value = "/controller-first", method = RequestMethod.GET)
public ModelAndView controller-first(  Locale locale,
                               HttpServletRequest request,
                               HttpServletResponse response,
                               Model model)
{   
 dinamically manipulate model here...     
 return new ModelAndView("static_resource_here");
} 

With this controller I have a form and submit that calls the second controller. The second controller should keep the ModelAndView from the first controller, modify the ModelAndView and send it to the user. Which is the best solution to implement such controller?

1 Answers

I got it. In the second controller I added a RedirectAttributes, then in the body I write:

 //for each attribute changed
 attributes.addFlashAttribute("attribute_name", model.getAttribute("name"));
 //now redirect to the first controller
 return new ModelAndView("redirect:/controller-first");
Related