Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

java - Spring MVC Controller: Redirect without parameters being added to my url

I'm trying to redirect without parameters being added to my URL.

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

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

After a redirect my url looks always something like this: .../success/?param1=xxx&param2=xxx. Since I want my URLs to be kind of RESTful and I never need the params after a redirect, I don't want them to be added on a redirect.

Any ideas how to get rid of them?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In Spring 3.1 a preferred way to control this behaviour is to add a RedirectAttributes parameter to your method:

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

It disables addition of attributes by default and allows you to control which attributes to add explicitly.

In previous versions of Spring it was more complicated.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...