I created two testing methods in the class MVC Controller. The first method Modelis passed as an argument, and in the second I create it directly. In both methods, I add one attribute to the instance Model:
@RequestMapping("/test-modelParam")
public String testMethod1(Model model) {
model.addAttribute("testname", "testvalue");
return "/testview";
}
@RequestMapping("/test-modelInstantiatedExplicitly")
public ModelAndView testMethod2() {
ModelAndView mav = new ModelAndView("/testview");
mav.addObject("testname", "testvalue");
return mav;
}
In both cases, the view is correctly populated.
Is there a difference between the two approaches? If so, where is it preferable to use one over the other?
source
share