简体   繁体   English

如何重定向到部署在本地服务器上的应用程序中的spring controller方法?

[英]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. 您可以使用HttpServletRequestHttpServletResponse对象上可用的方法来进行更与环境无关的重定向。 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.

相关问题 如何从Spring Controller重定向到本地页面? - How to redirect from spring controller to local page? 如果controller方法返回ResponseEntity,如何使用spring重定向 - How to use spring redirect if controller method returns ResponseEntity 在从jsp重定向上调用Spring控制器方法 - Invoke Spring controller method on redirect FROM jsp 如何从弹簧控制器重定向? - How to redirect from spring controller? 在部署到tomcat服务器的Spring Web应用程序中,catalina.properties文件由spring自动加载和检测。 怎么样? - In Spring web app deployed to tomcat server, catalina.properties file is automatically loaded and detected by spring. HOW? 无法使用通过 Tomcat 部署的 Spring 应用程序访问本地 MySQL 数据库 - Unable to local MySQL database with Spring app deployed through Tomcat Spring 引导:如何获取部署在 JBoss(或任何应用服务器)上的应用程序的运行端口? - Spring Boot: How to get running port of application deployed on JBoss (or any app server)? 当另一个控制器方法在Spring MVC中结束执行时,如何重定向到传递参数的控制器方法? - How can I redirect to a controller method passing a parameter when another controller method end its execution in Spring MVC? 使用Spring Controller方法发布到远程服务器 - Posting to a remote server in a spring controller method 如何将文件重定向到另一个spring控制器? - How to redirect file to another spring controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM