You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot项目Thymeleaf模板路径报错及白标错误求助

解决Spring Boot Thymeleaf模板找不到的问题

1. 确认Thymeleaf依赖是否正确引入

如果是Maven项目,检查pom.xml是否包含Thymeleaf starter依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Gradle项目则检查build.gradle:

implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

未引入则添加后重新构建项目。

2. 验证HTML文件位置与命名

  • 确保HTML文件存放在src/main/resources/templates/目录下,注意目录名拼写(不要写成template少了末尾的s)。
  • 控制器返回的视图名称需与HTML文件名完全匹配(比如控制器返回"index",对应index.html),避免特殊字符或大小写不一致问题。

3. 检查Thymeleaf配置

查看application.properties或application.yml中的模板路径配置,确保未错误修改默认值:

# 默认配置,请勿随意更改
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

若自定义了prefix,需保证路径与实际目录完全匹配。

4. 确认资源目录被正确识别

  • 在IDE中标记src/main/resources为资源根目录:IDEA右键目录→Mark Directory as→Resources Root;Eclipse右键→Build Path→Use as Source Folder。
  • 构建项目后,检查target/classes/templates/目录下是否存在编译后的HTML文件,若不存在则说明资源未被正确打包,需检查构建工具配置。

5. 清理并重建项目

  • 执行Maven命令:mvn clean compile,或Gradle命令:clean build。
  • 重启Spring Boot应用,避免缓存导致的异常。

6. 检查控制器返回逻辑

确保使用@Controller而非@RestController(@RestController会直接将返回值作为JSON响应,不会解析视图),示例代码:

@Controller
public class HomeController {
    @GetMapping("/")
    public String home() {
        return "index"; // 对应templates/index.html
    }
}

内容的提问来源于stack exchange,提问作者Sandhya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 04:35:03