Spring MVC: passing the model as an argument to the VS controller method, creating it explicitly

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?

+4
source share
1 answer

In the end, there is no difference; everything will end in ModelAndViewthe end.

Model ModelMap

  • @ModelAttribute
  • @SessionAttribute,

, , .

Model , / ModelAndView.

+4

All Articles