When to use ModelAndView vs Model in Spring?

后端 未结 3 1658
囚心锁ツ
囚心锁ツ 2020-12-07 16:23

This might sound dumb to the Spring experts, but I have to ask:
How do you decide on when to use ModelAndView and when to use Model

3条回答
  •  春和景丽
    2020-12-07 17:06

    One difference I can spot is with ModelAndView object you can set a direct reference to a view object:

    ModelAndView mav = ...
    mav.setView(myView);
    

    Whereas if you use Model and String, you need a view resolver to resolve the view name into an actual view

    public String myHandler(...) {
       return "myviewname"; // has to have a resolver from "myviewname" into an actual view
    }
    

提交回复
热议问题