spring boot 和 thymeleaf 仅用于电子邮件

发布于 2025-01-12 22:38:08 字数 1943 浏览 0 评论 0原文

我有一个可以正常工作的 spring boot 应用程序。

我需要添加一个功能,以便在发生某些操作时发送电子邮件。

我添加了 thymeleaf 代码:

@Configuration
public class ThymeleafTemplateConfig {

    @Bean
    public SpringTemplateEngine springTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        return templateEngine;
    }

    @Bean
    public FileTemplateResolver htmlTemplateResolver() {
        FileTemplateResolver emailTemplateResolver = new FileTemplateResolver();
        emailTemplateResolver.setPrefix(EMAILS_TEMPLATES_DIRECTORY);
        //emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML");
        emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return emailTemplateResolver;
    }

}

并在处理根 url 的代码下方/

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        //return "main.xhtml";
        return "operation.xhtml";
    }

}

当我登录到我的网站时,我重定向到“http://mysiteip/”,并且我已将“设置为映射” / " 是 operation.xhtml 因此,在重定向时或者如果我点击“http://mysiteip/”,则会发生错误。

 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - : - : - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "operation.xhtml", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "operation.xhtml", template might not exist or might not be accessible by any of the configured Template Resolvers

为什么thymeleaf尝试渲染视图?我只想将 thymeleaf 用于电子邮件部分(发送带有 html 正文的电子邮件)。

I have a spring boot application that works correctly.

I need to add a functionality to send emails if certain actions happens.

I have added the thymeleaf code:

@Configuration
public class ThymeleafTemplateConfig {

    @Bean
    public SpringTemplateEngine springTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        return templateEngine;
    }

    @Bean
    public FileTemplateResolver htmlTemplateResolver() {
        FileTemplateResolver emailTemplateResolver = new FileTemplateResolver();
        emailTemplateResolver.setPrefix(EMAILS_TEMPLATES_DIRECTORY);
        //emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode("HTML");
        emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return emailTemplateResolver;
    }

}

and below the code for handling the root url /

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index() {
        //return "main.xhtml";
        return "operation.xhtml";
    }

}

When I login to my site I redirect to "http://mysiteip/" and I have set as mapping that the "/ " is the operation.xhtml
so at the redirection or if I hit "http://mysiteip/" an error occurs.

 ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - : - : - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "operation.xhtml", template might not exist or might not be accessible by any of the configured Template Resolvers] with root cause
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "operation.xhtml", template might not exist or might not be accessible by any of the configured Template Resolvers

Why the thymeleaf tries to render the view? I want to use thymeleaf only for the email part (send emails with html body).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文