Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative path for an image in Thymeleaf

I want render PDF from Thymeleaf template. I have some issue with specifying path for the image that stored in resources/static/images/writing.png

<img th:src="@{/images/writing.png}" alt=""></img>

Code above works well if I open page at a web browser but it throws exception when I try process it with the TemplateEngine

Link base "/images/writing.png" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext interface (template: "TimetablePDF" - line 64, col 26)

I have an option to specify full path at src like http://localhost/images/... but I think its awful, I dont want do that.

So how can I specify it without hardcoding?

like image 891
ALEX Avatar asked Nov 07 '25 13:11

ALEX


1 Answers

This works for me

 <img th:src="@{classpath:static/images/writing.png}" />
like image 127
Huu Phuong Vu Avatar answered Nov 09 '25 08:11

Huu Phuong Vu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!