Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between th:href and href when linking absolute URLs in Thymeleaf

Tags:

html

thymeleaf

Right at the beginning of Thymeleaf documentation about standard url syntax there are two examples, but there is nothing said about the difference between them:

<a th:href="@{http://www.thymeleaf/documentation.html}">

<a href="http://www.thymeleaf/documentation.html">

Is there a difference between the two? If no, what is the use of the first one?

like image 807
handris Avatar asked Mar 13 '17 22:03

handris


1 Answers

In that specific case, there is no difference.

<a th:href="@{http://www.thymeleaf/documentation.html}">

will produce exactly

<a href="http://www.thymeleaf/documentation.html">

It's only there to because those sections are listing the different types of urls that url expressions work with (absolute, context relative, server relative and protocol relative). That being said, there are reasons you might use it... such as including an id in an absolute url. Something like:

<th:block th:with="id=${42774564}">
  <a th:href="@{https://stackoverflow.com/questions/{id}(id=${id})}">Stack Overflow</a>
</th:block>
like image 56
Metroids Avatar answered Oct 30 '22 13:10

Metroids