Spring RedirectView behaves differently in different tomcat installations

I have 2 tomcat instances. both are behind apache proxies. my code in Spring controller is as follows:

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

In 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

In another tomcat installation (which is on Linux) the redirection works relative to the context path, and the user ends up at http://somedomain2.dev/myapp/mySuperSexyStuff , which is obviously wrong.

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

All the URLs in my application are absolute (everything including links in jsps, redirect URLs and all places where links are used). I guess that is not the right way to do things: if I need to implement the HTTPS version on the site, I will have problems. Therefore, if you think that I should change something in my approach, please point me in the right direction.

+4
source share
3 answers

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

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

There is a constructor in the redirect view that takes the logical context of Relative, so the following will do the opposite of the above:

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

Your entire url should be contextual if you really haven’t pointed to your site, therefore links to css, images, etc. should use <c:url /> tags in jsp to resolve paths.

+6
source

One way to achieve this is to use your web application deployed in the root directory, and not as a "myapp" context. This makes sense because you have separate domains set up. Just use all your files in the tomcat root folder.

0
source

Spring documentation for MVC says that the path will always refer to your context path.

As far as I can see, you have two options:

  • Using an absolute path in your redirect, for example, "http: // somedomain ...."

  • Verify the configuration in the context of the web application context

Hope this helps even further ...

0
source

Source: https://habr.com/ru/post/1337076/


All Articles