[英]How to redirect to spring controller method in app deployed on local server?
On my local Tomcat server I develop few projects. 在本地Tomcat服务器上,我开发了几个项目。 One of them I write in Spring MVC. 我在Spring MVC中编写了其中之一。 I want to redirect some request to another method of controller. 我想将一些请求重定向到另一个控制器方法。 I know that it's possible by use: return "redirect:/path/to/resource";
我知道可以通过使用: return "redirect:/path/to/resource";
but if I have deployed app on Tomcat I have to use prefix like: return "redirect:
/appname
/path/to/resource";
但是,如果我已在Tomcat上部署了应用程序,则必须使用前缀,例如: return "redirect:
/appname
/path/to/resource";
. 。 How to make it work on server with prefix and without? 如何使其在带前缀和不带前缀的服务器上工作?
It sounds like you're having trouble because you've got the app running as the root context in some environments and not in others. 听起来您遇到了麻烦,因为在某些环境中而不是在某些环境中,您已将应用程序作为根上下文运行。 You can use the methods available on the HttpServletRequest
and HttpServletResponse
objects to do a redirect that is more environment independent. 您可以使用HttpServletRequest
和HttpServletResponse
对象上可用的方法来进行更与环境无关的重定向。 An example Spring controller method would look something like this: 一个示例Spring控制器方法如下所示:
@RequestMapping("/test")
public void test(HttpServletRequest request, HttpServletResponse response) throws IOException {
String url = request.getScheme() + "://" +
request.getServerName() + ":" +
request.getServerPort() +
request.getContextPath() + "/path/to/resource";
response.sendRedirect(url);
}
The getContextPath
part in particular solves the problem you're talking about, it will just return /
if the app is running as the root context but will return /appname
if the app is running as appname
like in your example. 该getContextPath
特别是部分解决了你在谈论这个问题,它只会返回/
如果应用程序正在运行的根上下文,但将返回/appname
,如果应用程序正在运行的appname
在你的榜样等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.