简体   繁体   English

Spring 引导 Thymeleaf:无法从资源/模板下的子文件夹中选取模板

[英]Spring Boot Thymeleaf: Unable to pick up templates from sub-folders under resources/templates

I am using Thymeleaf v3.0.11.RELEASE Spring Boot v2.1.3.RELEASE, and I am facing issues with my templates placed under classpath:templates/folder1/folder2/.我正在使用 Thymeleaf v3.0.11.RELEASE Spring Boot v2.1.3.RELEASE,我的模板放在类路径下时遇到问题:templates/folder1/folder2/。

I tried the below approaches我尝试了以下方法

  • Setting up the property spring.thymeleaf.prefix to classpath:/templates/**/ , classpath:/templates/* and other similar patterns将属性spring.thymeleaf.prefix设置为classpath:/templates/**/ , classpath:/templates/*和其他类似模式
  • Tried adding the template resolver in application startup class.尝试在应用程序启动 class 中添加模板解析器。

     @Bean public TemplateEngine emailTemplateEngine() { final SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.addTemplateResolver(htmlTemplateResolver()); return templateEngine; } private ITemplateResolver htmlTemplateResolver() { final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setResolvablePatterns(Collections.singleton("/*")); templateResolver.setTemplateMode(TemplateMode.HTML); templateResolver.setCharacterEncoding("UTF-8"); templateResolver.setCacheable(false); return templateResolver;

    } }

These approaches aren't working and I am still getting error:这些方法不起作用,我仍然遇到错误:

"Error resolving template [template_name], template might not exist or might not be accessible by any of the configured Template Resolvers"

Am I missing something?我错过了什么吗? I just need to know the way to enable wildcards for the suffix.我只需要知道为后缀启用通配符的方法。

Note: It works if I hard code classpath:templates/folder1/folder2 , but I can't since there are going to be multiple folders and I don't want to fixate all the folder names.注意:如果我硬编码classpath:templates/folder1/folder2 ,它会起作用,但我不能,因为会有多个文件夹,我不想固定所有文件夹名称。

add the following configuration class in your app 在您的应用中添加以下配置类

@Configuration
public class ThymeleafConfig {

  @Bean
  public SpringTemplateEngine springTemplateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(htmlTemplateResolver());
    return templateEngine;
  }

  @Bean
  public SpringResourceTemplateResolver htmlTemplateResolver() {
    SpringResourceTemplateResolver templateResolver =
        new SpringResourceTemplateResolver();
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(
        StandardTemplateModeHandlers.HTML5.getTemplateModeName());
    templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
    return templateResolver;
  }

}

Mine didn't work until I changed templateResolver.setPrefix("/WEB-INF/views/");在我更改templateResolver.setPrefix("/WEB-INF/views/"); to templateResolver.setPrefix("/WEB-INF/templates/");templateResolver.setPrefix("/WEB-INF/templates/"); and renamed the views folder to templates .并将 views 文件夹重命名为templates

I have no idea why but this is the way it is.我不知道为什么,但事实就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM