【问题标题】:Spring boot thymeleaf imagesSpring Boot thymeleaf 图片
【发布时间】:2017-04-14 19:21:51
【问题描述】:

我正在尝试为 sendind 电子邮件开发 Spring Boot 应用程序。一切都很好但是在模板百里香中,当我尝试添加图像时它显示错误。 这是我的 template.html 的 sn-p

 <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:remove="all">Order Confirmation</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div>

    <h2 th:text="${title}">title</h2>
    <p th:utext="${description}">
            description
    </p>

    <br />
    <br />
    <br />
    <p>Bien cordialement</p>
    <div>
        <img th:src="@{/img/signature.png}" />

    </div>
</div>

</body>
</html>

这是错误:

 ERROR [[/jira-rct/v1.0].[dispatcherServlet]] [http-nio-8080-exec-1] Servlet.service() for servlet [dispatcherServlet] in context with path [/jira-rct/v1.0] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Link base "/img/crm-signature.png" cannot be context relative (/) or page relative unless you implement the org.thymeleaf.context.IWebContext interface (context is of class: org.thymeleaf.context.Context) (email/template-1:20)] with root cause
org.thymeleaf.exceptions.TemplateProcessingException: Link base "/img/crm-signature.png" cannot be context relative (/) or page relative unless you implement the org.thymeleaf.context.IWebContext interface (context is of class: org.thymeleaf.context.Context) (email/template-1:20)
    at org.thymeleaf.standard.expression.LinkExpression.executeLink(LinkExpression.java:270) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:77) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:103) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:133) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.expression.Expression.execute(Expression.java:120) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.standard.processor.attr.AbstractStandardSingleAttributeModifierAttrProcessor.getTargetAttributeValue(AbstractStandardSingleAttributeModifierAttrProcessor.java:67) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.spring4.processor.attr.SpringSrcAttrProcessor.getTargetAttributeValue(SpringSrcAttrProcessor.java:68) ~[thymeleaf-spring4-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractSingleAttributeModifierAttrProcessor.getModifiedAttributeValues(AbstractSingleAttributeModifierAttrProcessor.java:59) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractAttributeModifierAttrProcessor.processAttribute(AbstractAttributeModifierAttrProcessor.java:62) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.4.RELEASE.jar:2.1.4.RELEASE]

最好的问候

【问题讨论】:

  • “src/main/resources/public”中有没有img文件夹?
  • 还有 img/crm-signature.png 与 img/signature.png 不同

标签: spring email spring-boot thymeleaf


【解决方案1】:

这对我有用^_^

用于Style/Image文件加载

<link rel="stylesheet" th:href="${basepath+'styles/test.css'}" />
<img th:src="${basepath+'img/test.png'}" />

那么您需要将基本路径从模型传递到您的视图

public void getPDF(HttpServletRequest request) throws DocumentException {
      String basepath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/";
      Context context = new Context();
      context.setVariable("fullname"," Soufiane ELAMMARI");
      context.setVariable("basepath", basepath);
}

【讨论】:

    【解决方案2】:

    这样使用:

    @Autowired
        ServletContext servletContext;
    
    @PostMapping("/getApplicationFastOpenDataById")
        @ResponseBody
        public String getApplicationOneById(@ModelAttribute ApplicationVO applicationVO, Principal principal
        , HttpServletRequest request, HttpServletResponse response) {
    
    WebContext webContext = new WebContext(request, response, servletContext);
    
    String fastOpenData = thymeleafTemplateEngine.process("/application/applicationFastOpening.html", webContext);
    
    }
    

    说明:

    context 必须知道你的 webapplication 状态

    所以使用 Webcontext 而不是上下文

    并传递httprequest、httpresponse、servletcontext

    谢谢

    【讨论】:

      【解决方案3】:

      为避免该错误,请添加服务器域。

      <img th:src="@{http://domain:8080/img/signature.png}" />
      

      以下代码可以正常工作:

      <img th:src="@{__${#httpServletRequest.scheme + '://' + #httpServletRequest.serverName + ':' + #httpServletRequest.serverPort}__/img/signature.png}" />
      

      【讨论】:

        【解决方案4】:

        对我来说这有效;只需删除“/”

        <img th:src="@{img/signature.png}"/>
        

        【讨论】:

        • 这不是一个好主意。这样做时,您将获得基于当前位置的相对路径。有时你在 ApplicationContext/ 但有时你在例如“ApplicationContext/Sub/Subsub/”,然后您的图片的 url 错误。
        【解决方案5】:

        文件夹结构应该是resources/static/img

        【讨论】:

          【解决方案6】:

          语法是正确的。现在验证您的项目中有下一个结构:

          ? src
            └─── ? main
                └─── ? resources
                    └─── ? public
                        └─── ? img
                            └─── signature.png
          

          您可以将图像存储在公共或静态文件夹下,两者都适用于 thymeleaf。

          【讨论】:

            【解决方案7】:

            通过以下路径将您的图像放入src/main/resources/static/img/signature.png

            然后在您的模板中将 URL 更改为以下内容:

            <img src="../static/img/signature.png" th:src="@{img/signature.png}"/>
            

            【讨论】:

            • 这不起作用我有这个错误 Undefined Image file ("../static/img/signature.png")。
            • 确保您的 Thymeleaf 已配置。因为您不应该看到../static/img/signature.png URL,所以它仅在静态模板开发时使用。 Thymeleaf 应该动态定义适当的 URL。
            • 我使用spring-boot默认配置。当我在其他模板中使用图像时,它没有问题。但是当我在这个模板中使用它来发送电子邮件时它不起作用
            • 创建一个演示此行为的最小示例,或将您当前的项目上传到某处。没有足够的信息来回答您的问题。
            • @AmineHatim 我知道这是不久前的事了,但我想知道你是否找到了解决这个问题的方法。我有同样的问题。该图像适用于其他模板,但不适用于电子邮件模板。这真的很烦人:)