Thymeleaf org.thymeleaf.exceptions.TemplateInputException: Error resolving template [xxx]

今日在学习Spring Boot thymeleaf 中踩了个坑,始终报错  Thymeleaf org.thymeleaf.exceptions.TemplateInputException: Error resolving template [Success.]

我们知道thymeleaf在查找资源的时候默认会到templates文件夹下,查找“.html”结尾的文件。而我在 templates 文件夹下已经创建了一个文件 “success.html”,但是始终报错,我的Controller层代码如下:

@Controller
public class HelloController {

    
    @RequestMapping("/success")
    public String success(){
        //classpath:/templates/success.html
        return "Success.";
    }
}

经过反复查询才发现问题的根本还是对thymeleaf的工作原理掌握不明白,一味的照搬教程,导致了错误发生。

thymeleaf在查找资源时会默认到templates下查找“.html”的文件来加载,如果templates下有多个html文件那么该加载哪个呢?这时候thymeleaf会根据Controller中方法的返回值来定位具体文件,也就是说Controller方法返回值就是具体的文件名!

知道了这个原理再检查代码发现 我的返回值多写了一个小数点 "."  而正是这个小数点导致了thymeleaf会去templates下查找名字为“success..html”的文件,自然是找不到的。

return "Success.";

实际上,报错信息也给了明显的提示,只是刚开始学不了解原理没有注意到提示信息中的小数点 “.”

Error resolving template [Success.]

学习任何一门技术首先要了解其中的原理,不能放过每一个细节,这样才能学的扎实。

原文地址:https://www.cnblogs.com/leasonYin/p/12252074.html