I have created two test methods in my MVC Controller
class. In the first method the Model
is being passed as an argument and in the second one I instantiate it directly. In both methods I add a single attribute to the Model
instance:
@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;
}
The View gets populated correctly in both cases.
Is there any difference between the two approaches? If so, Where is it preferred to use one over the other?