如何在我的jhipster应用程序中为Thymeleaf配置添加pdfTemplateResolver

时间:2018-05-31 10:42:01

标签: thymeleaf jhipster

我有一个使用jhipster生成的应用程序,它使用Thymeleaf来呈现电子邮件模板。一切都很好。

我想添加一个pdf生成器,我将使用百日咳模板。所以我想添加一个ClassLoaderTemplateResolver。我有这个:

@Configuration
public class ThymeleafConfiguration {

  @SuppressWarnings("unused")
  private final Logger log = LoggerFactory.getLogger(ThymeleafConfiguration.class);

  @Bean
  @Description("Thymeleaf template resolver serving HTML 5 emails")
  public ClassLoaderTemplateResolver emailTemplateResolver() {
    ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
    emailTemplateResolver.setPrefix("mails/");
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode("HTML5");
    emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
    emailTemplateResolver.setOrder(1);
    return emailTemplateResolver;
  }

  @Bean
  @Description("Thymeleaf template resolver serving HTML 5 emails")
  public ClassLoaderTemplateResolver pdfTemplateResolver() {
    ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
    emailTemplateResolver.setPrefix("pdf/");
    emailTemplateResolver.setSuffix(".html");
    emailTemplateResolver.setTemplateMode("HTML5");
    emailTemplateResolver.setCharacterEncoding(CharEncoding.UTF_8);
    emailTemplateResolver.setOrder(1);
    return emailTemplateResolver;
  }
}

这使得可以将模板放在不同的目录中。

我不清楚的是,您如何确保我获得pdf的pdf模板解析器和用于生成电子邮件的电子邮件模板解析器?

1 个答案:

答案 0 :(得分:0)

好吧,它们似乎都会被自动拾取。不确定模板名称在两个位置时会发生什么。为了避免这种情况,我可以像这样添加两个bean:

@Bean
public SpringTemplateEngine emailTemplateEngine() {
    final SpringTemplateEngine engine = new SpringTemplateEngine();
     final Set<TemplateResolver> templateResolvers = new HashSet<TemplateResolver>();
     templateResolvers.add(emailTemplateResolver());
     engine.setTemplateResolvers(templateResolvers);
    return engine;
}

@Bean
public SpringTemplateEngine pdfTemplateEngine() {
    final SpringTemplateEngine engine = new SpringTemplateEngine();
     final Set<TemplateResolver> templateResolvers = new HashSet<TemplateResolver>();
     templateResolvers.add(pdfTemplateResolver());
     engine.setTemplateResolvers(templateResolvers);
    return engine;
}