Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf : template might not exist or might not be accessible by any of the configured Template Resolvers

I have this code:

private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "templateemail.html";

@Bean
public TemplateEngine emailTemplateEngine() {
    templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(this.htmlTemplateResolver());
       )

    templateEngine.setTemplateEngineMessageSource(this.messageSource);
    return templateEngine;
}

private static ITemplateResolver htmlTemplateResolver() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setOrder(Integer.valueOf(0));
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateResolver.DEFAULT_TEMPLATE_MODE);
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    return templateResolver;
}


public void sendEmail(String emailAddress, String title, String body, Locale local, String image) {
    if (Boolean.parseBoolean(isEmailServiceActivated)) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mailMsg = new MimeMessageHelper(mimeMessage);
        try {
            mailMsg.setFrom(EMAIL_USERNAME);
            mailMsg.setTo(emailAddress);
            mailMsg.setSubject(title);

            // Prepare the evaluation context
            ctx.setLocale(local);
            ctx.setVariable("imageHeaderResourceName", HEADER_LOGO_IMAGE);
            ctx.setVariable("body", body);

            ctx.setVariable("imageResourceName", image);

            final String htmlContent = this.templateEngine.process(new ClassPathResource(EMAIL_INLINEIMAGE_TEMPLATE_NAME).getPath(), ctx);
            mailMsg.setText(htmlContent, true );

            mailMsg.addInline(HEADER_LOGO_IMAGE, new ClassPathResource(HEADER_LOGO_IMAGE ) , PNG_MIME);
            mailMsg.addInline(image, new ClassPathResource(image) , PNG_MIME);

        } catch (MessagingException e) {
            e.printStackTrace();
        }

        mailSender.send(mimeMessage);
    }
}

I have templateemail.html file under /templates/ directory. when I launch the sending email method I have this exception :

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "templateemail.html", template might not exist or might not be accessible by any of the configured Template Resolvers

I dont know if it's because the templateEngine can't find my file (I try even with tomcat absolute path and /bin directory but no way ) or I haven't configure the right Template Resolver. Thank you very much for your help. I

like image 899
MK-rou Avatar asked Apr 07 '17 14:04

MK-rou


3 Answers

It work now by deleting ".html" in the name of template (the file has the html extension)

private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "templateemail"
like image 124
MK-rou Avatar answered Sep 27 '22 22:09

MK-rou


Note the non-cross-platform behavior that can occure: on Windows the CamelCase.html template was resolved, but not on Ubuntu linux! There I had to rename it to camelcase.html, all chars in lower case

like image 31
Ilya Yevlampiev Avatar answered Sep 27 '22 22:09

Ilya Yevlampiev


I had the same issue recently. My problem was that my template had references to other templates that started with /.

For example:

<html ... th:include="/internal/layout-normal :: page">   <-- failed

<html ... th:include="internal/layout-normal :: page">    <-- worked

Both variants worked without problems when I run the application from IntelliJ. However, when packaged and run via java -jar the first line failed.

Removing the / solved the problem for me

like image 27
micha Avatar answered Sep 27 '22 22:09

micha