Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 521
user871611 Avatar asked Nov 06 '12 08:11

user871611


People also ask

How do I redirect a URL in spring?

We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.

What is the difference between forward and redirect in Spring MVC?

Forward: is faster, the client browser is not involved, the browser displays the original URL, the request is transfered do the forwarded URL. Redirect: is slower, the client browser is involved, the browser displays the redirected URL, it creates a new request to the redirected URL.

How do I forward a Spring MVC controller?

@Controller public class MainController { @Autowired OtherController otherController; @RequestMapping("/myurl") public String handleMyURL(Model model) { otherController. doStuff(); return ...; } } @Controller public class OtherController { @RequestMapping("/doStuff") public String doStuff(Model model) { ... } }


2 Answers

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.

like image 74
axtavt Avatar answered Oct 17 '22 13:10

axtavt


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

<mvc:annotation-driven ignoreDefaultModelOnRedirect="true" /> 
like image 42
Matroskin Avatar answered Oct 17 '22 14:10

Matroskin