java - 如何获取 thymeleaf 中的内容路径?

标签 java jquery spring-boot thymeleaf

我有一个使用 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/

相关文章:

java - Jackson 2 是否有相当于 Jackson 1 的 StdSerializerProvider 的?

java - 如何在Java中安全地将时间转换为unix时间戳?

javascript - 我可以将可拖动对象的助手而不是原始项目放入可排序对象中吗?

spring-boot - Spring 批量管理升级

java - 如何在java中不断添加你的值(int)?

java - 将事件转发给所有组件 JAVA

javascript 未在页面加载时执行?

jquery - 无法访问主干 View 中模板的 DOM 元素

java - Spring Boot 和 OVal 上的验证问题

java - 带 Map 键的 Spring Boot 映射