异常:Error resolving template "users/list", template might not exist or might not be accessible by...

spring.MVC调试时异常:Error resolving template “users/list”, template might not exist or might not be accessible by any of the configured Template Resolvers.

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "users/list", template might not exist or might not be accessible by any of the configured Template Resolvers
	at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:865) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608) ~[thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335) [thymeleaf-spring4-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring4-3.0.3.RELEASE.jar:3.0.3.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1282) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) [spring-webmvc-4.3.7.RELEASE.jar:4.3.7.RELEASE]

  • 项目开发环境:Eclipse+SpringBoot+thymeleaf;

最近在学习spring boot开发系统,本地测试开发页面都能正常显示,但启动gradle项目之后,/users无法访问到,百度且尝试了很多方法,最终成功解决,把查询到的方法做一个整理,方便日后查看:

问题一:提示模板页不存在,但实际能找到模板页,但在使用th:include标签的时候才会出错。
  • 解决:引用模板文件时不用“/”打头,通过类似相对路径的方式来引用,但这里的相对路径仍然是相对于模板根目录来做的。
@RequestMapping("/view")
public String view()  {
    return "/view";
}

去掉“/”:

    return "view";

问题二:注解错误,在controller上加注解@Controller 和@RestController都可以在前端调通接口,但是二者的区别如下:
  • 当用@Controller时在方法上必须添加注解@ResponseBody,不添加则报错。因为当使用@Controller 注解时,spring默认方法返回的是view对象(页面),加上@ResponseBody,则方法返回的就是具体对象。

  • @RestController的作用就相当于@Controller+@ResponseBody的结合体(如下图)
    在这里插入图片描述

    问题三及解决:确定模板是否在默认templates文件夹里面,并且路径要和返回的View名字一致(类似如下图):

    在这里插入图片描述
    各位已经学成或正在学习新知识的都一起加油加油~冲冲冲!!

<think>我们正在解决Thymeleaf的TemplateInputException,错误信息为“Error resolving template /system/file/uploadFile”。这个错误通常意味着Thymeleaf无法找到指定的模板文件。下面我们将逐步分析可能的原因和解决方案。 ### 1. 检查模板路径和名称 Thymeleaf默认的模板位置是`src/main/resources/templates/`,并且会自动添加`.html`后缀。因此,对于模板名为`uploadFile`,Thymeleaf会尝试在`templates`目录下查找`uploadFile.html`。 请确认: - 模板文件是否位于`src/main/resources/templates/system/file/`目录下? - 文件名是否为`uploadFile.html`(注意大小写,因为某些操作系统对文件名大小写敏感)? 如果路径不正确,请将模板文件移动到正确的位置。 ### 2. 检查控制器中的返回视图名称 在Spring MVC控制器中,返回的视图名称应该与模板文件的相对路径(相对于`templates`目录)匹配,且不带后缀。 例如,如果你的控制器方法返回视图名称为`"system/file/uploadFile"`,那么Thymeleaf会查找`templates/system/file/uploadFile.html`。 ```java @Controller public class FileController { @GetMapping("/upload") public String upload() { return "system/file/uploadFile"; // 这对应templates/system/file/uploadFile.html } } ``` 如果返回的视图名称包含前缀或后缀(如`"forward:..."`或`"redirect:..."`),则不会使用Thymeleaf解析。 ### 3. 检查Thymeleaf配置 在Spring Boot项目中,Thymeleaf的默认配置通常足够。但有时自定义配置可能导致问题。检查`application.properties`或`application.yml`中是否有以下配置: ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false # 开发时建议关闭缓存 ``` 确保这些配置没有被覆盖或修改。特别是,如果`prefix`被修改,则模板路径会变化。 ### 4. 检查模板解析器日志 启动应用时,Thymeleaf会打印配置信息。查看启动日志,确认Thymeleaf的`prefix`和`suffix`是否符合预期。 ### 5. 文件系统路径大小写敏感 在Linux系统中,文件路径是大小写敏感的。确保控制器中返回的视图名称与文件系统的实际路径大小写完全一致。 ### 6. 清理和重新构建项目 有时IDE可能没有及时更新资源文件。尝试清理并重新构建项目(例如,使用Maven的`mvn clean package`或Gradle的`gradle clean build`)。 ### 7. 检查依赖 确保项目中包含了`spring-boot-starter-thymeleaf`依赖(对于Maven项目): ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` ### 8. 检查模板文件内容 如果模板文件存在语法错误,也可能导致解析失败。但通常错误信息会不同。不过,为了排除可能性,可以尝试用一个简单的HTML文件替换,看是否能正常加载。 ### 9. 直接访问模板文件 如果上述步骤都无效,可以尝试在浏览器中直接访问模板文件(但需要确保Thymeleaf不拦截静态资源)。不过,通常模板文件不能直接访问,因为它们在`templates`目录下(受保护)。但你可以检查静态资源目录(`src/main/resources/static/`)下是否有同名文件,可能会引起冲突。 ### 总结 按照以上步骤逐一排查,通常可以解决Thymeleaf模板解析失败的问题。重点检查模板文件的位置和名称、控制器返回的视图名称、以及Thymeleaf配置。 §§相关问题§§ 1. Thymeleaf的默认模板路径是什么? 2. 如何在Spring Boot中配置Thymeleaf的模板后缀? 3. 如何解决Thymeleaf模板中的变量解析错误? 4. Thymeleaf模板缓存对开发有什么影响?</think>针对Thymeleaf的`TemplateInputException: Error resolving template /system/file/uploadFile`错误,以下是系统排查步骤和解决方案: ### 1. **检查模板路径与命名** - **物理路径验证** 确认模板文件位于正确目录: ```bash src/main/resources/templates/system/file/uploadFile.html ``` *注意:Linux系统严格区分大小写,确保路径中的`system/file`与实际目录大小写完全一致*。 - **控制器返回值匹配** Spring控制器返回的视图名必须与**物理路径相对`templates/`的部分**一致: ```java @Controller public class FileController { @GetMapping("/upload") public String showUploadPage() { return "system/file/uploadFile"; // 不带.html后缀 } } ``` ### 2. **验证Thymeleaf配置** - **检查`application.properties`** 确保未覆盖默认配置: ```properties # 默认配置(无需显式声明) spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false # 开发环境关闭缓存 ``` - **启动日志确认** 在应用启动日志中搜索: ```log ThymeleafTemplateEngine [TemplateResolver=templateResolver] Prefix: classpath:/templates/ Suffix: .html ``` ### 3. **依赖与构建问题** - **Maven/Gradle依赖** 检查是否包含必要依赖: ```xml <!-- Maven --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` - **资源文件打包** 确认构建后`uploadFile.html`存在于jar/war包中: ```bash jar tvf target/your-app.jar | grep "system/file/uploadFile.html" ``` ### 4. **特殊场景处理** - **重定向与转发** 避免在返回值中添加前缀: ```java // 错误示例 return "forward:/system/file/uploadFile"; // 正确示例 return "system/file/uploadFile"; ``` - **自定义视图解析器冲突** 检查是否有额外的`ViewResolver` bean覆盖了Thymeleaf默认解析器。 ### 5. **调试步骤** - **直接URL访问测试** 临时将模板移到静态目录测试: ```bash mv src/main/resources/templates/system/file/uploadFile.html \ src/main/resources/static/temp.html ``` 访问 `http://localhost:8080/temp.html` 验证文件是否可读。 - **启用DEBUG日志** 在`application.properties`中添加: ```properties logging.level.org.thymeleaf=DEBUG ``` ### 常见修复方案总结 | 问题类型 | 解决方案 | |----------|----------| | 路径大小写错误 | 重命名目录为全小写:`/system/file/` → `/system/file/` | | 缓存问题 | 设置`spring.thymeleaf.cache=false`并清理`target/`目录 | | 构建遗漏 | 执行`mvn clean package`重新打包 | | 视图名错误 | 检查控制器返回值是否包含非法前缀(如`/`, `forward:`) | > **关键提示**:95%的此类错误由**路径大小写不匹配**或**控制器返回错误视图名**导致。优先检查这两项[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值