【问题标题】:difference between redirect and redirect inside modelandviewmodel和view内部redirect和redirect的区别
【发布时间】:2013-10-22 11:33:53
【问题描述】:

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

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

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

请哪位大神解释一下这两种说法的异同。

以及它必须在什么情况下使用。


罗希特:

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

【问题讨论】:

  • 他们都做同样的工作。返回 ModelAndView 是 Spring 2.0 之前存在的旧方式。

标签: java spring spring-mvc


【解决方案1】:

声明:

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

做同样的事情: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

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 2017-11-24
    • 2012-03-15
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 2019-02-01
    • 2022-12-13
    相关资源
    最近更新 更多