今天使用Springboot集成thymeleaf返回页面时的结果一不小心出现了 Error resolving template [login.html], template might not exist or might not be accessible by any of the configured Template Resolvers这个错误,最后瞄瞄发现是一个常识性的错误,就结合其他时候踩到的坑就和大家分享一下。首先看错误
对就是这个错,出现这个错,一般是路径的问题,所以你看一下你的路径首先你需要先看一下你的配置文件
spring
thymeleaf:
prefix: classpath:/templates/
encoding: UTF-8
mode: HTML5
然后在看一下自己的控制器
@RequestMapping("/login")
public String getLoginIndex() {
return "login";
}
这样整就对了。但是偷偷的告诉你们,其实Springboot默认配置 prefix: classpath:/templates/,之前我也不太清楚,知道一次着急没有配置的时候但是页面显示正常,就发现了这个问题,小伙伴们可以试试,还有的小伙伴们说可以去掉配置最后面的/,即 prefix: classpath:/templates,我悄悄的告诉你这样你要多写好多/,因为你要把控制器所有返回的加上/,即return “/login”;是不是不划算?最后再说个糗事,这个错还有一个问题就是你的页面是在static文件夹里面而不是templates文件夹里面,不要问我为啥知道。