繁体   English   中英

在modelandview内重定向和重定向之间的区别

[英]difference between redirect and redirect inside modelandview

在spring控制器类中重定向到url

  • 有些地方都使用return "redirect:/abc.htm";

  • 还使用return new ModelAndView("redirect:/abc.htm")

任何人请解释两个陈述的差异和相似之处。

它必须在哪种情况下使用。


罗希特:

我使用RedirectAttribute从旧网址获取值。 在这种情况下,在使用此return "redirect:/abc.htm";时获取值return "redirect:/abc.htm"; 但不在此return new ModelAndView("redirect:/abc.htm")
RedirectAttributes有什么区别吗?

声明:

return "redirect:/abc.htm"
return new ModelAndView("redirect:/abc.htm")

做同样的事情: redirects请求redirectsabc.htm 如果返回具有前缀redirect:的视图名称,则会将其识别为需要重定向的特殊指示。 视图名称的其余部分将被视为重定向URL。

随着声明

return "redirect:/abc.htm"

您只能返回重定向视图名称。

使用ModelAndView您可以在单个返回值中返回modelview

ModelAndView modelAndView =  new ModelAndView("redirect:/abc.htm");
modelAndView.addObject("modelAttribute" , new ModelAttribute());
return modelAndView;

但是,客户端(浏览器)将为URL /abc.htm创建的新重定向请求中的属性值不可用。 ModelAndView的最佳用途是将请求转发到新URL,以便您可以在单个返回值中同时返回modelview 对于重定向方案,如果要传递属性,则应使用RedirectAttributes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM