代码之家  ›  专栏  ›  技术社区  ›  tibi

如何在jhipster应用程序中为thymeleaf配置添加pdftemplateresolver

  •  0
  • tibi  · 技术社区  · 6 年前

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

    我想添加一个pdf生成器,我将使用thymeleaf模板。所以我想添加一个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的模板解析和生成电子邮件的电子邮件模板解析?

    1 回复  |  直到 6 年前
        1
  •  0
  •   tibi    6 年前

    好吧,好像都是自动接的。不确定当模板名同时出现在这两个位置时会发生什么。为了避免这种情况,我可以添加两个这样的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;
    }