I am redirecting from a controller to another controller. But I also need to pass model attributes to the second controller.
I don\'t want to put the model in sessi
By using @ModelAttribute we can pass the model from one controller to another controller
[ Input to the first Controller][1]
[]: https://i.stack.imgur.com/rZQe5.jpg from jsp page first controller binds the form data with the @ModelAttribute to the User Bean
@Controller
public class FirstController {
@RequestMapping("/fowardModel")
public ModelAndView forwardModel(@ModelAttribute("user") User u) {
ModelAndView m = new ModelAndView("forward:/catchUser");
m.addObject("usr", u);
return m;
}
}
@Controller
public class SecondController {
@RequestMapping("/catchUser")
public ModelAndView catchModel(@ModelAttribute("user") User u) {
System.out.println(u); //retrive the data passed by the first contoller
ModelAndView mv = new ModelAndView("userDetails");
return mv;
}
}