How to use explicit links with Thymeleaf?

Viewed 245

I wanted to make and link to external site in Spring+Thymeleaf web application and inserted link like this: <a th:href="@{https://example.com}">Link</a>

However by clicking the link it tries to point internal location http://localhost/service/https://example.com not external website https://example.com like it supposed to.

Site gives just an error "status":500,"error":"Internal Server Error","message":"The request was rejected because the URL was not normalized."

What's wrong and how to make absolute links with Thymeleaf?

2 Answers

The problem was that the application was built on top of other Spring+Thymeleaf project. Only way I got link working was like this.

<a th:text="link" th:onclick="|window.location.href='@{https://example.com}'|"></a>

I know this is not a "real" link and cannot be configure to open with target="_blank" or so but it's working with this use case.

If the link is a static one You can simply use <a href="https://example.com">Link</a>

If it is a dynamic one, You can use <a th:href="${link}">Link</a> in the template. And in Controller return new ModelAndView("page").addObject("link", <link>)

Related