Spring mvc 为什么我们需要RedirectAttributes.addAttribute()?

Spring mvc 为什么我们需要RedirectAttributes.addAttribute()?,spring-mvc,Spring Mvc,Spring提供以下选项,通过使用RedirectAttributes将属性(参数)传递到重定向页面 @Controller @RequestMapping("/") public class RedirectController { @GetMapping("/redirectWithRedirectView") public ModelAndView redirectWithUsingRedirectView(RedirectAttributes attributes) {

Spring提供以下选项,通过使用RedirectAttributes将属性(参数)传递到重定向页面

@Controller
@RequestMapping("/")
public class RedirectController {

    @GetMapping("/redirectWithRedirectView")
    public ModelAndView redirectWithUsingRedirectView(RedirectAttributes attributes) {
        attributes.addAttribute("attribute", "redirectWithRedirectView");
        return new ModelAndView("redirect:redirectedUrl");
    }
} 
即使我们只是将属性附加到重定向URL,也可以实现同样的效果,如下所示

@Controller
@RequestMapping("/")
public class RedirectController {

    @GetMapping("/redirectWithRedirectView")
    public ModelAndView redirectWithUsingRedirectView() {
        return new ModelAndView("redirect:redirectedUrl?attribute=redirectWithRedirectView");
    }
} 

两者都做同样的工作。如果我们使用重定向属性,在内存方面有什么好处吗?我猜,在第二种情况下,如果每个请求的属性值发生变化,我们将构建单独的ModelAndView对象

它们为您将属性转换为字符串,为您编码,为您附加到查询字符串,还允许存储flash属性。它们为您将属性转换为字符串,为您编码,为您附加到查询字符串,还允许存储flash属性。