我有一个使用 springboot 1.5.10、eclipse IDE 和 Thymeleaf 的项目。
在几个页面中,我在 jquery 中有这样的重定向:
例如:在选择选项中
$('#month').on('change', function() {
window.location = "/dashboard/operation/month/" + month;
})
例如:在按钮中
$('#bFind').click(function() {
var newUrl = "/dashboard/operation/month/";
newUrl += month;
$('a').attr('href', newUrl);
});
这两个重定向在我的 Controller 中定义得很好。
网址是: http://localhost:8080/dashboard/operation/month/01
但问题是,当我创建一个 war 项目并将其部署在我的 apache tomcat 9.0.6 中时,我必须指定一个名称来部署。
部署后,网址为: http://localhost:8080/mywebapp/dashboard/operation/month/01
它破坏了东西......
mywebapp 可以是另一个名称。
所以我一直在寻找添加上下文路径:
我找到的主要信息是:
What is the Syntax to get Thymeleaf ${pageContext.request.contextPath}
但是当我将其添加到可能的 header.html 文件时,我总是未定义
<meta name="ctx" th:content="${#httpServletRequest.getContextPath()}" />
也许还有另一种方法可以做到这一点并正确添加上下文路径。
有什么建议吗?
谢谢
最佳答案
https://www.thymeleaf.org/doc/articles/standardurlsyntax.html
也许试试这个:
<meta name="ctx" th:content="@{/}" />
如果上下文是localhost:8080,则会输出
<meta name="ctx" content="/" />
但是如果你的上下文是 localhost:8080/mywebapp 它将输出
<meta name="ctx" content="/mywebapp/" />
关于java - 如何获取 thymeleaf 中的内容路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49630770/