Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring RedirectView behaving differently in different tomcat installations

I have 2 tomcat instances. both are behind proxying apache httpds. my code in the Spring controller looks like this:

@RequestMapping(value = "/doSuperSexyStuff", method = RequestMethod.GET)
public String viewSuperSexyStuff() {
    return "redirect:/mySuperSexyStuff";
}

On my first tomcat installation on Windows I have somedomain1.dev redirected to http://localhost:8080/myapp and everything works flawlessly. redirect goes to http://somedomain1.dev/mySuperSexyStuff

On the other tomcat installation (which is on Linux) the redirect works relative to the context path and user end up at http://somedomain2.dev/myapp/mySuperSexyStuff which is obviously wrong.

What should I do for spring to ignore the context path and just redirect the user to where he "belongs"?

All URLs in my application are absolute(everything including links in jsps, redirect urls and all the places where links are used). I guess that's not the correct way to do stuff: if I have to implement HTTPS version of the site I'll be in trouble. So if you you think I must change something fundamentally in my approach please point me in the right direction.

like image 806
Yervand Aghababyan Avatar asked Jan 26 '11 00:01

Yervand Aghababyan


1 Answers

Instead of returning a String, which is very inflexible, consider returning a View:

@RequestMapping(value = "/doSuperSexyStuff", method = RequestMethod.GET)
    public View viewSuperSexyStuff(){
    return new RedirectView("/mySuperSexyStuff");
}

The redirect view has a constructor which takes a boolean contextRelative so the following would do the opposite of the above:

return new RedirectView("/mySuperSexyStuff", true);

All your url's should be context relative unless you really them point off your site, so links to css, image assets etc. should in jsp's use <c:url /> tags to resolve paths.

like image 77
dezzer10 Avatar answered Sep 24 '22 02:09

dezzer10