3

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.

Orkun
  • 6,998
  • 8
  • 56
  • 103
  • check the structure and syntax here: https://stackoverflow.com/questions/41586841/thymeleaf-cssspringboot – DimaSan Feb 23 '18 at 21:30
  • @DimaSan Thanks for answer, i ve tried it exactly as mentioned, but it doesnT work with my case, i guess it s due to the context. – Orkun Feb 26 '18 at 07:20
  • maybe yes, but it's really hard to say, probably if you could create a minimal example and share the link to GitHub, we would be able to help you. – DimaSan Feb 26 '18 at 17:14
  • 1
    was this ever solved? having the same issue – jbailie1991 Mar 28 '18 at 13:35
  • I ended up hardcoding the img in the HTML template i have in base64 – Orkun Mar 28 '18 at 14:17

1 Answers1

1

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;
    }
});

Brett Y
  • 7,171
  • 1
  • 28
  • 42