Spring RedirectView在不同的tomcat安装中表现不同

Yer*_*yan 5 java redirect tomcat servlets spring-mvc

我有2个tomcat实例.两者都在代理apache httpds.我在Spring控制器中的代码如下所示:

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

在我在Windows上的第一次tomcat安装中,我将somedomain1.dev重定向到http://localhost:8080/myapp,一切都运行良好.重定向去http://somedomain1.dev/mySuperSexyStuff

在另一个tomcat安装(在Linux上),重定向相对于上下文路径工作,用户最终http://somedomain2.dev/myapp/mySuperSexyStuff显然是错误的.

我该怎么做才能让spring忽略上下文路径,只是将用户重定向到"所属"的位置?

我的应用程序中的所有URL都是绝对的(包括jsps中的链接,重定向URL和使用链接的所有位置).我想这不是正确的做法:如果我必须实施网站的HTTPS版本,我将遇到麻烦.所以,如果你认为我必须在我的方法中从根本上改变一些事情,请指出我正确的方向.

dez*_*r10 8

而不是返回一个非常不灵活的String,考虑返回一个View:

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

重定向视图有一个构造函数,它接受一个布尔的contextRelative,所以下面的代码与上面相反:

return new RedirectView("/mySuperSexyStuff", true);
Run Code Online (Sandbox Code Playgroud)

你的所有url都应该是上下文相关的,除非你真的指出你的网站,所以链接到css,图像资产等应该在jsp的use <c:url />标签中解析路径.