39

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?

Roman C
  • 49,761
  • 33
  • 66
  • 176
user871611
  • 3,307
  • 7
  • 51
  • 73

6 Answers6

34

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.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 3
    Thanks, works like a charm! Since I have many redirects and find it anyoing to have to add unused RedirectAttributes-parameters to all my contoller-actions: It would be nice if there was a way to configure this 'behavior' in my Spring setup as default. – user871611 Nov 06 '12 at 08:52
31

In Spring 3.1 use option ignoreDefaultModelOnRedirect to disable automatically adding model attributes to a redirect:

<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" />
Matroskin
  • 326
  • 3
  • 2
  • is there an equialent for this on spring 3.0 ? – Federico Lenzi Jul 12 '13 at 18:12
  • 2
    Assuming that rv is an instance of `org.springframework.web.servlet.view.RedirectView`, one could set this per-RedirectView instance for more fine grained control: `rv.setExposeModelAttributes();` – reallynice Sep 03 '14 at 12:02
  • 6
    For Spring >= 4.0 use ignore-default-model-on-redirect="true" – Peter Clause Oct 10 '14 at 07:30
  • 3
    I think disabling it by default is a great idea. You never know what kind of sensitive information you may have your model that you do not intend on making public. – Zoidberg Nov 27 '14 at 16:54
13

Adding RedirectAttributes parameter doesn't work for me (may be because my HandlerInterceptorAdapter adds some stuff to model), but this approach does (thanks to @reallynic's comment):

@RequestMapping("save/")
public View doSave(...)
{
    ...
    RedirectView redirect = new RedirectView("/success/");
    redirect.setExposeModelAttributes(false);
    return redirect;
}
Community
  • 1
  • 1
Ilya Serbis
  • 21,149
  • 6
  • 87
  • 74
9

In Spring 4 there is a way to do this with java config, using annotations. I'm sharing it in case anyone needs it as I needed it.

On the config class that extends WebMvcConfigurerAdapter, you need to add:

@Autowired
private RequestMappingHandlerAdapter requestMappingHandlerAdapter;


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

With this, you do not need to use RedirectAttributes, and it is an equivalent in java config to Matroskin's answer.

Carrol
  • 1,225
  • 1
  • 16
  • 29
6

If you're using Spring 3.1, you can use Flash Scope, otherwise you can take a look at the method used in the most voted (not accepted) answer here:

Spring MVC Controller redirect using URL parameters instead of in response

EDIT:

Nice article for 3.1 users:

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

Workaround for non-3.1 users:

Spring MVC custom scope bean

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
2

Try this:

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;
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 5
    Welcome to Stack Overflow! While this code snippet may solve the question, including an explanation of *how* and *why* this solves the problem [would really help](//meta.stackexchange.com/q/114762) to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Feb 03 '17 at 13:10