如何解析 Thymeleaf 模板以通过电子邮件发送?
How do I resolve a Thymeleaf template to send in an email?
我正在我的 Spring 引导项目中设置电子邮件验证。我正在关注来自 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 实例,因为它需要 HttpServletRequest
、HttpServletResponse
和 ServletContext
参数用于 constructor。这些对象可以在我的控制器中访问,但不能在我的事件监听器中访问。
是否可以在事件侦听器中处理我的 Thymeleaf 模板?
像 /webjars/bootstrap/5.0.1/css/bootstrap.min.css
这样的相对引用在电子邮件中根本不起作用(因为电子邮件不在原始服务器上,相对 css 引用没有意义)。在这种情况下,您的选择(在删除相关 links 之后)是:
包括 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>
将 link 更改为绝对值 URL(这可能有效也可能无效,因为某些客户端删除了这些引用)。
<style href="https://yourserver/webjars/bootstrap/5.0.1/css/bootstrap.min.css" />
我正在我的 Spring 引导项目中设置电子邮件验证。我正在关注来自 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 实例,因为它需要 HttpServletRequest
、HttpServletResponse
和 ServletContext
参数用于 constructor。这些对象可以在我的控制器中访问,但不能在我的事件监听器中访问。
是否可以在事件侦听器中处理我的 Thymeleaf 模板?
像 /webjars/bootstrap/5.0.1/css/bootstrap.min.css
这样的相对引用在电子邮件中根本不起作用(因为电子邮件不在原始服务器上,相对 css 引用没有意义)。在这种情况下,您的选择(在删除相关 links 之后)是:
包括 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>
将 link 更改为绝对值 URL(这可能有效也可能无效,因为某些客户端删除了这些引用)。
<style href="https://yourserver/webjars/bootstrap/5.0.1/css/bootstrap.min.css" />