7

I am trying to inject a domain url into a link using Thymeleaf. My url is being passed from the controller because i put logging and saw it.

my link is as such in my Thymeleaf html template:

<link type="text/css" th:href="@{${DomainUrl}/web/assets/css/foundation5/foundation.min.css}" rel="stylesheet" />

however, when I run it locally it doesn't replace the domain, for example, throws an error (because the URL is not found of course) render as such: http://localhost:8081/pss/ui/$%7BDomainUrl%7D/web/assets/css/components.css

OakvilleWork
  • 2,377
  • 5
  • 25
  • 37
  • Should work just like that. Maybe `DomainUrl` view variable is not set?? – Antoniossss Oct 27 '17 at 18:16
  • I do add it as such and logged to make sure it is being populated.. mav.addObject("DomainUrl", ctx.getDomainUrl()); – OakvilleWork Oct 27 '17 at 18:32
  • add `[[${DomainUrl}]]` as text node (simple text in html) to your markup, is that rendering given domain? – Antoniossss Oct 27 '17 at 18:33
  • yes it does print it. i found out that there was a base url tag which was why it was putting in the domain, however, i removed that and when it is a link as my example: still does not populated the correct domain, perhaps it is a tag in the controller that is making the links relative. any idea on what Spring bean i can look for? – OakvilleWork Oct 27 '17 at 20:01
  • so you need relative or absolute cuz im lost now? chceck thymeleaf docs on that topic as well. maybe one of # beans will help – Antoniossss Oct 27 '17 at 20:30

3 Answers3

7

Ok so in order for this to work you must use preprocess operator __expression__ to get propert link so you will end up with somethink like this

<link type="text/css" th:href="@{__${DomainUrl}__/web/assets/css/foundation5/foundation.min.css}" rel="stylesheet" />

this will preprocess and resolve ${DomainUrl} expression, and will pass resulting string to to @ expression processor. Tested and work like charm:

<a th:href="@{__${currentUrl}__/blah/blah/blahhhh}">hey there</a>

generates

<a href="http://localhost:8080/admin/place/list/blah/blah/blahhhh">hey there</a> 

where http://localhost:8080/admin/place/list/ is currentUrl

in my

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
2

I found the solution for it. I had to make the link into literal as such:

<link type="text/css" th:href="@{|${DomainUrl}/web/assets/css/foundation5/normalize.css|}" rel="stylesheet" />
OakvilleWork
  • 2,377
  • 5
  • 25
  • 37
0

Try

<link type="text/css" th:href="@{${DomainUrl + '/web/assets/css/foundation5/foundation.min.css'}}" rel="stylesheet" />

Does ${DomainUrl} start with http:// or https://? That makes a difference when creating a link with @{} expressions.

Metroids
  • 18,999
  • 4
  • 41
  • 52
  • can you elaborate more on the difference – Antoniossss Oct 27 '17 at 21:01
  • When you say "absolute url", that has a specific meaning -- that it starts with `http://` or `https://`. See the thymeleaf documentation: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#link-urls . As for the link I made, you can't mix unquoted strings and variables like you did. That's why I put the rest of the url within `${}`. – Metroids Oct 27 '17 at 21:15
  • ahh i see what you ment. I understood thet https and http makes a difference here.... alternating schemes are confusion here – Antoniossss Oct 27 '17 at 21:53