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

问题描述 投票:0回答:2

我创建了一个 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
2个回答
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 模板是否有有效的配置位置。

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

如果您最终决定do想要使用Thymeleaf,则需要通过在运行时类路径上提供默认文件夹

templates
或通过属性文件指定自定义位置来解决此问题。


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


0
投票

如上所述,错误是因为您已将 Thymeleafe 作为依赖项包含在 pom.xml 中,但未提供要运行的模板。如果您将 thymeleaf 作为依赖项包含在内,那么我认为最好提供模板而不是抑制消息。

这是文件路径和文件示例。添加它并重新运行您的项目,它应该可以正常工作。

src/main/resources/templates/example.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>Example Title</title>
</head>

<body>

<p th:text="Welcome to the example paragraph"/>

</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.