Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf : relative url - without a webcontext

I get this infamous error:

cannot be context relative (/) or page relative unless you implement the IWebContext

I have a spring boot application (without the web module) that creates pdf files.

I am planning to use an HTML file as a template, but I could not link the css file nor the image properly due to these url issues.

Html :

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

  <head>
    <title>Company Invoice</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" 
          href="./css/company.css" th:href="@{./css/company.css}"/>
  </head>
  <body>
    <p th:utext="#{home.welcome}">Welcome !</p>
     <img src="/images/gtvglogo.png" th:src="@{/images/gtvglogo.png}"/>
  </body>    
</html>

folder structure:

src/main/resources/templates/sample.html
src/main/resources/templates/css/sample.css

I googled a bit but I donT want to solve this via IWebContext. Is there another way?

Thanks in advance.

like image 239
Orkun Ozen Avatar asked Oct 16 '25 03:10

Orkun Ozen


1 Answers

org.thymeleaf.exceptions.TemplateProcessingException: Link base "/a/relative/link" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext interface (template: "templates/a-template" - line 6, col 13)

1 at org.thymeleaf.linkbuilder.StandardLinkBuilder.computeContextPath (StandardLinkBuilder.java:493)

...

The exception is thrown by the org.thymeleaf.linkbuilder.StandardLinkBuilder. By providing a different implementation of org.thymeleaf.linkbuilder.ILinkBuilder to the TemplateEngine we can avoid this expception

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setLinkBuilder(new ILinkBuilder() {

    @Override
    public String getName() {
        return null;
    }

    @Override
    public Integer getOrder() {
        return null;
    }

    @Override
    public String buildLink(IExpressionContext context, String base, Map<String, Object> parameters) {
        return null;
    }
});

like image 163
Brett Y Avatar answered Oct 18 '25 01:10

Brett Y



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!