Controller handler method supported return types

前端 未结 3 1360
醉酒成梦
醉酒成梦 2021-01-24 06:46

While learning the Spring framework, I notice in the book Spring in Action, the author doesn\'t use ModelandView method return type in controllers. The aut

3条回答
  •  佛祖请我去吃肉
    2021-01-24 07:39

    Functionality wise there is no difference, both these are equivalent:

    @RequestMapping(..)
    public String requestMapping1(Model model){
        model.addAttribute("attr1", attr1);
        return "viewName";
    }
    
    @RequestMapping(..)
    public ModelAndView requestMapping2(){
        ModelAndView modelAndView = new ModelAndView("viewName");
        modelAndView.addObject("attr1", attr1);
        return modelAndView;
    }
    

    However the preferred approach is the former and that is the reason why the author has not used the latter in the book samples.

提交回复
热议问题