解析模板时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问

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

我有以下控制器:

@Controller
public class ConfigController {

    @GetMapping("/")
    public String index() {
        return "config";
    }
}

但是,我在前往路线 / 时收到以下错误:

There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [config], template might not exist or might not be accessible by any of the configured Template Resolvers

我在 resources/templates 中有一个 config.html 文件:

我的 pom.xml 文件:https://pastebin.com/yqYYuXWh

我做错了什么?

我已经试过在controller函数的return中加一个.html,还是不行


删除 spring-web 依赖项后(我已经有 spring-boot-starter-web)我现在在启动时收到以下错误 spring:

Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.3.20</version>
    </dependency>

不需要第二个依赖项。


    <build>
<!--not required. Spring boot scans /resources directory by default 
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.properties</include>
                </includes>
            </resource>
        </resources>
-->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

请重建并重新启动应用程序。

而且您不需要在控制器中添加 .html 扩展。 Spring boot 默认在 src/main/resource/ ,src/main/resource/templates/ ,src/main/resource/static/ ,src/main/resource/public 这些目录中寻找 .html,.jsp 扩展

问题出在您的 pom.xml 文件中。

通过将此块添加到您的 build;

<resources>
    <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
            <include>application.properties</include>
        </includes>
    </resource>
</resources>

您已经覆盖了 spring-boot-starter-parent 自己的 resources 块并使其包含 application.properties 而没有其他内容。从您的 pom.xml 中删除它并删除您的目标目录。