简体   繁体   English

Thymeleaf 无法检测到 spring-boot 项目中的模板

[英]Thymeleaf cannot detect templates inside spring-boot project

I have the following project structure in my Spring boot app, in which I want to use Thymeleaf我的 Spring boot 应用程序中有以下项目结构,我想在其中使用 Thymeleaf

projectName
    -Gradle-Module1(Spring boot module)
        -build
        -src
            -main
            -resources
                -templates
                    index.html
        build.gradle
    -Gradle-Module2
        ...
    build.gradle
    ...

but the spring-boot cannot find my template directory and is showing warning但 spring-boot 找不到我的模板目录并显示警告

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)找不到模板位置:classpath:/templates/(请添加一些模板或检查您的 Thymeleaf 配置)

PS: I am using @EnableAutoConfiguration PS:我正在使用@EnableAutoConfiguration

In my controller code I am doing something like:在我的控制器代码中,我正在做类似的事情:

@Controller
@EnableAutoConfiguration
public class BaseController {

    @RequestMapping(value = "/")
    public String index() {
        return "index.html";
    }
}

and index.html file just prints hello world.index.html文件只打印 hello world。

So typically it should look inside src/resources/templates/ (of same Gradle module I suppose), but somehow it is not able to find it.所以通常它应该查看src/resources/templates/ (我想是同一个 Gradle 模块),但不知何故它无法找到它。

When I try to access localhost:8080 I am getting below error当我尝试访问localhost:8080时,出现以下错误

Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers`解析模板“index.html”时出错,模板可能不存在或可能无法被任何配置的模板解析器访问

Is there anything I am missing?有什么我想念的吗?
Any pointers appreciated.任何指针表示赞赏。 Thanks.谢谢。

You have to configure Thymeleaf as follows:您必须按如下方式配置 Thymeleaf:

@Configuration
public class ThymeleafConfig {
    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setCacheable(false);
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        return templateResolver;
    }

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

    @Bean
    public ThymeleafViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setOrder(1);
        return viewResolver;
    }
}

Spring doc recommends to add @EnableAutoConfiguration annotation to your primary @Configuration class. Spring 文档建议@EnableAutoConfiguration注释添加到您的主要@Configuration类。

Also it seems you have wrong project structure, the typical package hierarchy is:此外,您似乎有错误的项目结构,典型的包层次结构是:

src
  |- main
      |- java
      |- resources
          |- static
          |- templates
  |- test

In this case your templates will be in src/main/resources/templates , not in src/resources/templates/ .在这种情况下,您的模板将在src/main/resources/templates ,而不是在src/resources/templates/

You should only return the file name.您应该只返回文件名。 Eg without the .hmtl例如没有 .hmtl

@RequestMapping(value = "/")
   public String index() {
   return "index";
}
@GetMapping("/")
public String index() {
    return "index";
}

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

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