【问题标题】:Spring Boot / Docker: Thymeleaf template not found for html mailSpring Boot / Docker:找不到 html 邮件的 Thymeleaf 模板
【发布时间】:2020-11-21 06:51:01
【问题描述】:

我在 docker 上查找 thymeleaf 模板时遇到问题。我熟悉普通控制器中的leading slash problem。不过,我现在想使用 thymeleaf 来呈现电子邮件正文。

我的代码如下所示:

@Component
public class HtmlEmailDocumenter implements Documenter {

    @Autowired
    SpringTemplateEngine thymeleafTemplateEngine;

    // more dependencies

    @Override
    public void accept(Documentable documentable){
        Context thymeleafContext = new Context();
        thymeleafContext.setVariable("doc", documentable);

        String template = "mail/default";
        String htmlBody = thymeleafTemplateEngine.process(template, thymeleafContext);

        // send the email...
    }
}

有问题的模板位于/src/main/resources/templates/mail/default.html 下。我也曾尝试在源代码中使用“mail/default.html”。在 docker 中也不起作用。

欢迎任何提示。

【问题讨论】:

    标签: java spring spring-boot docker thymeleaf


    【解决方案1】:

    我通过如下定义模板引擎模板和解析器在非Boot Spring应用程序中做类似的事情:

      @Bean
      public ResourceBundleMessageSource emailMessageSource() {
        final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("templates/email/mailMessages");
        return messageSource;
      }
    
      @Bean
      public TemplateEngine emailTemplateEngine() {
        final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        templateEngine.setTemplateEngineMessageSource(emailMessageSource());
        return templateEngine;
      }
    
      private ITemplateResolver htmlTemplateResolver() {
        final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setOrder(Integer.valueOf(2));
        templateResolver.setPrefix("/templates/email/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
      }
    

    然后,我只使用 /templates/email 目录中的模板名称来引用电子邮件消息模板,不带扩展名。

    【讨论】:

      猜你喜欢
      • 2017-06-21
      • 2017-05-10
      • 2016-01-26
      • 2020-08-27
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2021-03-16
      • 2021-09-04
      相关资源
      最近更新 更多