Issue
Is there any way to prevent StackOverflowException when adding model into modelandview?
I have a method that returns a modelandview but I've seen that if I try to add model to modelandview it throws and StackOverflowException because Model already has modelandview, is there any way to add it without need to remove modelandview from model first?
((BindingAwareModelMap) model).remove("org.springframework.validation.BindingResult.modelAndView");
((BindingAwareModelMap) model).remove("modelAndView");
modelAndView.addObject(model);
return modelAndView;
Solution
When you use ModelAndView
, Spring does not expect you to manually create a Model
.
Spring expects that you set the attributes that you want for model to have directly on the model object contained inside ModelAndView
.
So it should be
modelAndView.getModel().put("attribute1", object1);
modelAndView.getModel().put("attribute2", object2);
Answered By - Panagiotis Bougioukos
Answer Checked By - Marilyn (JavaFixing Volunteer)