如何在Thymeleaf中访问contextPath?

Jac*_*aby 3 thymeleaf spring-boot

我想在没有设置org.springframework.ui.Model的情况下访问thymeleaf中的contextPath.我在application.properties文件中配置了上下文路径

Ash*_*ena 7

如果您使用的是Thymeleaf,您应该可以使用

<a th:href="@{/XXX}">Link Name</a> 
Run Code Online (Sandbox Code Playgroud)

假设已经宣布.

我认为这需要使用SpringTemplateEngine.


Met*_*ids 5

的url表达式可@{/}为您提供完整的上下文...例如,如果您具有:

# application.properties
server.contextPath=/your/context
Run Code Online (Sandbox Code Playgroud)

那你可以用

<div th:text="@{/}" />
or
<div th:text="${#request.contextPath}" />
both of which will output
<div>/your/context/</div>
Run Code Online (Sandbox Code Playgroud)