I need to create an absolute URL for my SPRING application using Thymeleaf

advertisements

I have a SPRING application running (using spring boot) either directly on Java or on a Tomcat 7 server. I need to create absolute URL's for a couple of pages (mappings) so that these links can be sent via e-mail.

I thought this would be simple, but now it seems hard. I would prefer a solution that is 100% written in Thymeleaf, but if that is not possible, I can certainly provide Thymeleaf with some variables from my Java code.

Anyone solved this before in Thymeleaf?


In my opinion, you should provide a server URL in a property file and than access it in Thymeleaf. You can do it by accessing Spring Bean in a view:

<div th:text="${@urlService.getApplicationUrl()}">...</div>

In the above example, the urlService is a Spring Bean.

Please see this answer: https://stackoverflow.com/a/675903/718515

You may be also interested in #ctx variable that gives you access to servletContext. See Thymeleaf docuementation: http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#base-objects

I hope it helps!