在Thymeleaf中链接绝对URL时,th:href和href之间的区别

在Thymeleaf中链接绝对URL时,th:href和href之间的区别

问题描述:

就在Thymeleaf的开头文档有关标准url语法的信息例子,但是它们之间的区别却没有说:

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?

在这种情况下,没有区别.

In that specific case, there is no difference.

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

将完全产生

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

仅此而已,因为这些部分列出了URL表达式可使用的不同类型的URL(绝对,上下文相关,服务器相关和协议相关).话虽这么说,但您可能会使用它是有原因的...例如,在绝对URL中包含一个ID.像这样:

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>