如何在没有在 application.properties 中设置的情况下获取控制器中的上下文路径 [英] How to get context path in controller without set in application.properties
问题描述
我想知道是否可以在不设置 application.properties 中的值的情况下获取控制器中的上下文路径
I would like to know if it possible to get the context path in my controller without set the value in application.properties
server.contextPath
当我创建一个战争以在 apache tomcat 服务器中部署时,我得到如下内容:
When I create a war to deploy in an apache tomcat server I get something like this:
mywebapp-0.0.1-SNAPSHOT.war
很简单,我将名称更改为 "mywebapp" 并进行部署.
so just simple I change name to "mywebapp" and deploy.
但如果我想更改为 "mywebapp2" 并运行两个 web 应用程序,我需要更改 server.contextPath 中的值.
But if I want to change to "mywebapp2" and have two webapps running I need to change the value in server.contextPath.
我想做一些我在百里香中做的事情:
I would like to do something like I do in thymeleaf:
我在不担心名称的情况下获得上下文路径,而是在我的 java 控制器中使用百里香叶.
where I get the context-path without worring about the name, but instead of thymeleaf in my java controller.
谢谢
推荐答案
在控制器的处理程序方法中声明一个形参HttpServletRequest request
.然后调用 request.getContextPath.
Declare a formal parameter HttpServletRequest request
in the handler method of your controller. Then call request.getContextPath.
这篇关于如何在没有在 application.properties 中设置的情况下获取控制器中的上下文路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!