The statements:
return "redirect:/abc.htm"
return new ModelAndView("redirect:/abc.htm")
do the same thing: redirects
the request to abc.htm
. If a view name is returned that has the
prefix redirect:
, this is recognized as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL.
With the statement
return "redirect:/abc.htm"
you can only return the redirect view name.
With ModelAndView
you can return both model
and view
in a single return value:
ModelAndView modelAndView = new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;
But the attribute value will not be available in the new redirect request that the client(browser) will make for the URL /abc.htm
. The best use of ModelAndView
is when you forward the request to a new URL, so that you can return both model
and view
together in a single return value. For redirect scenarios, if you want to pass attributes, you should use RedirectAttributes
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…