春季靴子百里香的图像

时间:2016-11-30 12:52:36

标签: spring email spring-boot thymeleaf

我正在尝试为sendind电子邮件开发spring boot应用程序。一切都还可以但是在模板百里香中,当我尝试添加图像时显示错误。 这是我的template.html的片段

 <!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]

祝你好运

7 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

通过以下路径src/main/resources/static/img/signature.png将您的图片置于其中。

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

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

答案 3 :(得分:0)

文件夹结构应为resources/static/img

答案 4 :(得分:0)

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

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

以下代码可以正常工作:

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

答案 5 :(得分:0)

使用这种方式:

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

}

说明:

上下文必须知道您的Web应用程序状态

因此使用Webcontext代替上下文

并传递httprequest,httpresponse,servletcontext

thx

答案 6 :(得分:0)

这对我有用^ _ ^

用于加载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);
}