找不到模板位置:(请添加一些模板,检查您的 Thymeleaf 配置,或设置 spring.thymeleaf.check-template-location=false)

Cannot find template location :( please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)

提问人:Nimasha Madhushani 提问时间:10/27/2022 更新时间:5/27/2023 访问量:5412

问:

我创建了一个Spring Boot应用程序,在运行它时收到此警告。

2022-10-27 00:35:12.520  WARN 11512 --- [  restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)

在这里,你可以看到我的application.properties文件

spring.datasource.url=jdbc:mysql://localhost:3306/springbootblogapp? 
createDatabaseIfNotExist=true
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL8Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.show-sql=true
spring.jpa.open-in-view=false

尽管这里有一些相关的问题,但为它们提供的答案对我不起作用,因为那里提到的警告与我得到的警告有点偏离。

请帮帮我。

spring-boot maven intellij-idea thymeleaf

评论

0赞 Nimasha Madhushani 10/27/2022
根据您的建议,我添加了警告,然后警告已修复。感谢您的支持spring.thymeleaf.check-template-location=false
0赞 andrewJames 10/27/2022
很高兴它解决了,这是主要的事情 - 但这不是我的建议。这是警告消息中提供的建议之一。我只是重复了一遍。
0赞 Nimasha Madhushani 10/27/2022
是的。突然间,我无法捕捉到它。谢谢你强调它。现在我正在罚款我们需要添加该代码行的原因。你知道原因吗?

答:

1赞 andrewJames 10/27/2022 #1

我最初的评论,转移到一个答案:

以下是您的问题中显示的警告消息的文本:

2022-10-27 00:35:12.520 WARN 11512 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates, check your Thymeleaf configuration, or set spring.thymeleaf.check-template-location=false)

您是否尝试了警告消息所建议的内容 - 例如,通过添加:

spring.thymeleaf.check-template-location=false 

到您的应用程序的配置?或者通过创建警告消息中提到的目录(并在那里添加模板)?默认情况下,应在资源位置创建此目录,以便它在运行时位于应用程序的类路径上。templates


根据您的后续评论进行更新:

为什么会收到此警告?

创建 Spring Boot 应用程序时,可以选择各种其他依赖项(例如,通过使用 Spring Initializr)。

一个这样的额外依赖项是 Thymeleaf。

对于 Maven(假设您使用的是 Maven),这是 POM 中的以下工件:

<artifactId>spring-boot-starter-thymeleaf</artifactId>

如果您不设置要存储 Thymeleaf 模板的基本位置,则选择此选项是没有意义的。

如果选择配置该选项:

spring.thymeleaf.check-template-location=false

那么你就告诉 Spring 不要费心检查你的 Thymeleaf 模板是否有有效的配置位置。

因此,即使没有有效的配置位置,您也不会看到该警告。

如果您决定要使用 Thymeleaf,则需要通过在运行时类路径上提供默认文件夹或通过属性文件指定自定义位置来解决这个问题。templates


您可能会看到与您选择的其他 Spring Boot 依赖项类似的警告(甚至错误)。例如,如果您选择包含 JDBC API,但没有配置有效的数据源,那么我认为这会导致错误(如果我记得的话)。

评论

0赞 Nimasha Madhushani 10/27/2022
是的,一个超级酷的答案。感谢您的进一步澄清。