【发布时间】:2021-08-25 17:32:00
【问题描述】:
我正在我的一个 Spring Boot 项目中设置电子邮件验证。我正在关注来自 baeldung 的 this tutorial。我想使用 Thymeleaf 模板发送 HTML 电子邮件。在浏览了互联网之后,我决定为我的RegistrationListener 自动装配一个SpringTemplateEngine 实例并使用它来处理模板:
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 实例,因为它需要 constructor 的 HttpServletRequest、HttpServletResponse 和 ServletContext 参数。这些对象可以在我的控制器中访问,但不能在我的事件侦听器中访问。
是否可以在事件监听器中处理我的 Thymeleaf 模板?
【问题讨论】:
-
事情就是这样。像这样的相对引用在电子邮件中不起作用。 gmail 服务器不会包含
bootstrap.min.css的副本,您的电子邮件可以通过相对链接引用该副本。你必须摆脱那些参考。将 css 直接包含在文件中(在 -
@Metroids 在bootstrapemail.com 的帮助下听从您的建议,我能够得到想要的结果。如果您将您的评论作为答案,我会接受。
标签: java spring-boot spring-security thymeleaf