Thymeleaf:无需应用程序上下文路径即可获取href

时间:2019-05-29 09:40:05

标签: thymeleaf

如何获取没有上下文路径的URL? 我正在使用Spring Boot 1.5.18.RELEASE和Thymeleaf

我尝试了这两种可能性。

<a th:href="@{${website}}" target="_blank"> Click here </a>

<a th:href="${website}" target="_blank"> Click here </a>

例如,如果$ {website}的值为www.example.com,我会得到以下结果:

http://localhost:8081/MyApp/www.example.com 但我只想获取此www.example.com,而没有上下文路径。

2 个答案:

答案 0 :(得分:0)

我这样解决:

<th:block th:with="link=${website}">
         <a th:href="@{//{link}(link=${link})}" target="_blank">&gt; Click here</a>
</th:block>

答案 1 :(得分:0)

如果要链接到外部站点,则应将http://https://作为链接的一部分-https://www.example.com而不是www.example.com。添加完后,您最初的帖子中的两种解决方案都可以使用。

<a th:href="@{${website}}" target="_blank">Click here</a>
<a th:href="${website}" target="_blank">Click here</a>
您已经指出的

//也可以使用,但这有点愚蠢。这只是意味着,如果您的站点为https://,则链接将为https://(对于http://则相反)。

如果您不想将https://添加到链接中,我会建议这样的内容:

<a th:href="|https://${website}|" target="_blank">&gt; Click here</a>