Spring MVC + Thymeleaf - 正确的上下文路径处理

ano*_*mer 1 java spring spring-mvc thymeleaf

我正在使用Spring MVC和Thymeleaf开发Web应用程序.无论应用程序在何处部署,使页面链接工作的正确方法是什么?例如,如果我写

<a href="/about">Link</a>
Run Code Online (Sandbox Code Playgroud)

部署我的应用程序不会/,但在/app/链接将无法正常工作.

我想在每个Controller方法中获取当前路径:request.getContextPath()而不是在页面上的每个链接中使用它,但也许有一种方法可以做得更好?

Nic*_*ero 5

如果您正在使用Thymeleaf,您应该能够使用已声明的<a th:href="@{/about}">Link</a>假设<html xmlns:th="http://www.thymeleaf.org">.我认为这确实需要使用SpringTemplateEngine.