Thymeleaf Context-relative URLs

Thymeleaf provides an easy way to create URLs using link expressions @{...}.

In this article, we will use context-relative URLs in Thymeleaf template.

Thymeleaf Context-relative URLs

This kind of URLs are the most used ones in web applications. Context-relative URLs are relative to the web application root context configured on the server. 
For example, if your Spring Boot application using context path, so there is a server.contextPath=/myapp parameter in your application.properties, the myapp will be the context name.
Context-relative URLs start with /.
For example, link provided like the following:

<a th:href="@{/tag/thymeleaf}">Thymeleaf</a>

for application served on myapp context, the output will look like the following:

<a href="/myapp/tag/thymeleaf">Thymeleaf</a>

for application served without root context, the output will be the following:

<a href="/tag/thymeleaf">Thymeleaf</a>


Comments