如何解析要在电子邮件中发送的thymeleaf模板?

jhdbpxl9  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(447)

我正在我的一个spring boot项目中设置电子邮件验证。我将在贝尔东学习本教程。我想用thymeleaf模板发送一封html电子邮件。在浏览了互联网之后,我决定自动连接一个 SpringTemplateEngine 我的例子 RegistrationListener 并使用它来处理模板:

Context context = new Context(event.getLocale());
context.setVariable("token", token);

String html = thymeleafTemplateEngine.process("account/verify_email", context);

但是,此方法不起作用,因为我的模板引用了相关资源:

org.thymeleaf.exceptions.TemplateProcessingException: Link base "/webjars/bootstrap/5.0.1/css/bootstrap.min.css" cannot be context relative (/...) unless the context used for executing the engine implements the org.thymeleaf.context.IWebContext interface (template: "account/verify_email" - line 3, col 7)

看到异常消息,我决定探索实现异常的类 IWebContext . 最好的比赛是 WebContext . 但是,我不能简单地创建webcontext示例,因为它需要 HttpServletRequest , HttpServletResponse ,及 ServletContext 构造函数的参数。这些对象可以在控制器中访问,但不能在事件侦听器中访问。
是否可以在事件侦听器中处理my thymeleaf模板?

qco9c6ql

qco9c6ql1#

相对引用,如 /webjars/bootstrap/5.0.1/css/bootstrap.min.css 根本不在电子邮件中工作(因为电子邮件不在原始服务器上,相对css引用没有意义)。在这种情况下,您的选项(删除相对链接后)是:
包括css内联:

<style>
 /* Put the contents of /webjars/bootstrap/5.0.1/css/bootstrap.min.css  here */
 </style>

只需直接使用内联css修改标记:

<div style="font-family: sans-serif; color:#666666;>Your content here.../div>

将链接更改为绝对url(这可能有效,也可能无效,因为某些客户端删除了这些引用)。

<style href="https://yourserver/webjars/bootstrap/5.0.1/css/bootstrap.min.css" />

相关问题