MVCaraiman
MVCaraiman

Reputation: 629

Construct an html anchor for a Thymeleaf variable holding an abolute url (external to the app)

I have a variable in my Thymeleaf context, called r. r has a getUrl that returns an URL, something like www.a.co I want to create an HTML anchor http://www.a.co Is there any better way of doing it in Thymeleaf ? My solution is the following, but I don't really like it.

<a th:href="@{http://{path}(path=${r.url})}">   
    <span th:text="${r.url}"/> 
</a>

Upvotes: 1

Views: 1298

Answers (1)

Metroids
Metroids

Reputation: 20477

There are a lot of different ways of doing this (depending on your needs). I guess I would prefer:

<a th:href="|http://${r.url}|" th:text="${r.url}" /> 

You might also be interested in one of these:

<a th:href="${'http://' + r.url}" th:text="${r.url}" /> 
<a th:href="'http://' + ${r.url}" th:text="${r.url}" /> 
<a th:href="@{${'http://' + r.url}}" th:text="${r.url}" /> 

Upvotes: 5

Related Questions