spring Thymeleaf :模板可能不存在或可能无法被任何配置的模板解析器访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43280097/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:23:40  来源:igfitidea点击:

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

springtomcatthymeleaf

提问by MK-rou

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 :

我在 /templates/ 目录下有 templateemail.html 文件。当我启动发送电子邮件方法时,我有这个例外:

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

我不知道是不是因为 templateEngine 找不到我的文件(我什至尝试使用 tomcat 绝对路径和 /bin 目录但没有办法),或者我没有配置正确的模板解析器。非常感谢您的帮助。一世

回答by MK-rou

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

它现在可以通过删除模板名称中的“.html”来工作(该文件具有 html 扩展名)

private static final String EMAIL_INLINEIMAGE_TEMPLATE_NAME = "templateemail"

回答by Ilya Yevlampiev

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

请注意可能发生的非跨平台行为:在 Windows 上,CamelCase.html 模板已解决,但在 Ubuntu linux 上未解决!在那里我不得不将它重命名为camelcase.html,所有字符都是小写

回答by micha

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 -jarthe first line failed.

当我从 IntelliJ 运行应用程序时,这两种变体都没有问题。但是,当打包并通过java -jar第一行运行时失败。

Removing the /solved the problem for me

删除/解决了我的问题