【问题标题】:Spring MVC Controller: Redirect without parameters being added to my urlSpring MVC 控制器:重定向而不将参数添加到我的 url
【发布时间】:2012-10-26 04:22:07
【问题描述】:

我正在尝试重定向而不向我的 URL 添加参数。

@Controller
...
public class SomeController
{
  ...
  @RequestMapping("save/")
  public String doSave(...)
  {
    ...
    return "redirect:/success/";
  }

  @RequestMapping("success/")
  public String doSuccess(...)
  {
    ...
    return "success";
  }

重定向后,我的 url 看起来总是这样:.../success/?param1=xxx&param2=xxx。 因为我希望我的 URL 是 RESTful 的,并且在重定向后我永远不需要参数,所以我不希望它们被添加到重定向中。

有什么办法可以摆脱它们吗?

【问题讨论】:

    标签: java spring-mvc restful-url


    【解决方案1】:

    在 Spring 3.1 中,控制此行为的首选方法是在您的方法中添加 RedirectAttributes 参数:

    @RequestMapping("save/")
    public String doSave(..., RedirectAttributes ra)
    {
        ...
        return "redirect:/success/";
    }
    

    它默认禁用添加属性,并允许您控制要显式添加的属性。

    在以前的 Spring 版本中,它更复杂。

    【讨论】:

    • 谢谢,就像一个魅力!由于我有很多重定向并且发现必须将未使用的 RedirectAttributes 参数添加到我所有的控制器操作中:如果有一种方法可以在我的 Spring 设置中将这种“行为”配置为默认设置,那就太好了。
    【解决方案2】:

    在 Spring 3.1 中使用选项 ignoreDefaultModelOnRedirect 禁用自动将模型属性添加到重定向:

    <mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />
    

    【讨论】:

    • 在 spring 3.0 上是否有一个等价的?
    • 假设 rv 是 org.springframework.web.servlet.view.RedirectView 的一个实例,可以设置这个 per-RedirectView 实例以获得更细粒度的控制:rv.setExposeModelAttributes(&lt;false or true&gt;);
    • 对于 Spring >= 4.0 使用 ignore-default-model-on-redirect="true"
    • 我认为默认禁用它是个好主意。您永远不知道您的模型可能有哪些您不打算公开的敏感信息。
    【解决方案3】:

    添加RedirectAttributes 参数对我不起作用(可能是因为我的 HandlerInterceptorAdapter 向模型添加了一些东西),但这种方法可以(感谢@reallynic 的comment):

    @RequestMapping("save/")
    public View doSave(...)
    {
        ...
        RedirectView redirect = new RedirectView("/success/");
        redirect.setExposeModelAttributes(false);
        return redirect;
    }
    

    【讨论】:

      【解决方案4】:

      在 Spring 4 中,有一种方法可以通过 java config 使用注解来做到这一点。我正在分享它,以防有人需要它,因为我需要它。

      在扩展WebMvcConfigurerAdapter的配置类上,需要添加:

      @Autowired
      private RequestMappingHandlerAdapter requestMappingHandlerAdapter;
      
      
      @PostConstruct
      public void init() {
          requestMappingHandlerAdapter.setIgnoreDefaultModelOnRedirect(true);
      }
      

      有了这个,你就不需要使用RedirectAttributes,它在java config中相当于Matroskin的答案。

      【讨论】:

        【解决方案5】:

        如果你使用的是 Spring 3.1,你可以使用Flash Scope, 否则您可以在此处查看投票最多(未接受)的答案中使用的方法:

        Spring MVC Controller redirect using URL parameters instead of in response

        编辑:

        3.1 用户的好文章:

        http://www.tikalk.com/java/redirectattributes-new-feature-spring-mvc-31

        非 3.1 用户的解决方法:

        Spring MVC custom scope bean

        【讨论】:

          【解决方案6】:

          试试这个:

          public ModelAndView getRequest(HttpServletRequest req, Locale locale, Model model) {
          
              ***model.asMap().clear();*** // This clear parameters in url
          
              final ModelAndView mav = new ModelAndView("redirect:/test");
          
              return mav;
          }
          

          【讨论】:

          • 欢迎来到 Stack Overflow!虽然这段代码 sn-p 可以解决问题,包括说明 如何为什么 这解决了问题would really help 以提高您的帖子质量。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-01-29
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多