【问题标题】:Thymeleaf : relative url - without a webcontextThymeleaf : 相​​对 url - 没有 webcontext
【发布时间】:2018-08-03 09:55:50
【问题描述】:

我收到了这个臭名昭著的错误:

不能是上下文相关 (/) 或页面相关,除非您实现 IWebContext

我有一个创建 pdf 文件的 spring boot 应用程序(没有 web 模块)。

我打算使用 HTML 文件作为模板,但由于这些 url 问题,我无法正确链接 css 文件和图像。

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>

文件夹结构:

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

我用谷歌搜索了一下,但我不想通过 IWebContext 解决这个问题。 还有其他方法吗?

提前致谢。

【问题讨论】:

  • @DimaSan 感谢您的回答,我已经完全按照上述方法进行了尝试,但它不适用于我的情况,我想这是由于上下文的原因。
  • 也许是的,但真的很难说,也许如果你能创建一个最小的例子并分享到 GitHub 的链接,我们就能帮助你。
  • 这个问题解决了吗?有同样的问题
  • 我最终在 base64 中的 HTML 模板中对 img 进行了硬编码

标签: spring thymeleaf


【解决方案1】:

org.thymeleaf.exceptions.TemplateProcessingException:链接基础 “/a/relative/link”不能是上下文 相对 (/...) 除非用于执行引擎的上下文 实现 org.thymeleaf.context.IWebContext 接口(模板: “模板/模板” - 第 6 行,第 13 列)

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

...

异常由org.thymeleaf.linkbuilder.StandardLinkBuilder 引发。通过为 TemplateEngine 提供 org.thymeleaf.linkbuilder.ILinkBuilder 的不同实现,我们可以避免这个异常

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

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多