【问题标题】:How to make spring not to add all model attributes to a redirect URL?如何让spring不将所有模型属性添加到重定向URL?
【发布时间】:2014-10-05 00:57:56
【问题描述】:

在我们经常使用 return new ModelAndView(new RedirectView("/url")) 从 Spring 控制器返回重定向之前,使用老式(扩展 *Controller)控制器时。

升级到 Spring 4.0 (@Controller) 后,我们开始使用 return "redirect:/url" 从 @RequestMapping 注释的方法中执行相同的操作。然而,我们注意到 Spring 将 all 模型属性添加到生成的重定向 URL。这对我们来说是非常不受欢迎的行为。

更糟糕的是:因为它们被添加到“位置”HTTP 标头(HTTP 302 的标准)中,这使得 Tomcat 抛出以下异常:

Aug 11, 2014 2:42:31 PM org.apache.coyote.ajp.AjpProcessor process
SEVERE: Error processing request
java.lang.ArrayIndexOutOfBoundsException: 8192
    at org.apache.coyote.ajp.AjpMessage.appendByte(AjpMessage.java:146)

想到的一个解决方案是在每次重定向之前清除模型,但这相当麻烦。

有没有办法配置 Spring 不将所有模型属性添加到重定向 url?

【问题讨论】:

    标签: spring-mvc


    【解决方案1】:

    在一个新的应用程序中,或者如果您的应用程序不依赖于重定向模型的使用,您应该像 Aramir 所说的那样将 ignore-default-model-on-redirect="true" 添加到 <mvc:annotation-driven> 标记中。

    如果它只涉及少数需要传统模式的新方法,您可以在方法中添加RedirectAttribute 参数。一旦它存在,Spring 就会使用它而不是模型来确定应该在重定向 URL 中使用哪些属性。

    【讨论】:

      【解决方案2】:

      ignore-default-model-on-redirect="true" 添加到您的<mvc:annotation-driven> 标记中。

      【讨论】:

      • 谢谢!就是这样!
      • 但我希望仅对整个项目中的 ModelAndView 和控制器之一将此设置为 true。我该怎么做?
      猜你喜欢
      • 2011-10-22
      • 2020-06-29
      • 1970-01-01
      • 2018-06-26
      • 2018-02-27
      • 1970-01-01
      • 2016-02-15
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多