【问题标题】:How to get content path in thymeleaf?如何在百里香中获取内容路径?
【发布时间】:2018-09-12 19:42:15
【问题描述】:

我有一个项目使用 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);
});

在我的控制器中定义的两个重定向都很好。

网址是: http://localhost:8080/dashboard/operation/month/01

但问题是,当我创建一个战争项目以将其部署到我的 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()}" />

也许还有另一种方法可以做到这一点并正确添加上下文路径。

有什么建议吗?

谢谢

【问题讨论】:

  • 我认为这是您的客户端的问题,而不是服务器端的问题,在开头使用“/”设置位置总是返回服务器根目录 (localhost:8080/)

标签: java jquery spring-boot thymeleaf


【解决方案1】:

https://www.thymeleaf.org/doc/articles/standardurlsyntax.html

不妨试试这个:

&lt;meta name="ctx" th:content="@{/}" /&gt;

如果上下文是localhost:8080,就会输出

&lt;meta name="ctx" content="/" /&gt;

但是如果你的上下文是 localhost:8080/mywebapp 它会输出

&lt;meta name="ctx" content="/mywebapp/" /&gt;

【讨论】:

    猜你喜欢
    • 2018-03-15
    • 2019-08-10
    • 2017-10-21
    • 2018-04-25
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多