IDEA使用springboot+templates报错 Template might not exist or might not be accessible
1.创建的文件夹 并不是资源文件夹
有种情况是文件夹不是资源文件夹,所以找不到资源。下图红框为资源文件夹。博主就是放了这种疏忽大意的错误,突然想起来,问题就解决了。
2. 在controller层请求处理完了返回时,没有使用@RestController或@ResponseBody而返回了非json格式
这种情况下返回的数据thymeleaf模板无法解析,直接报错,本人正式因为这个原因才报错。
解决方案:可以将@Controller换成@RestController,不过需要注意有没有其他的方法返回了html页面,会导致返回的不是页面而是字符串;最好的方法就是在你所请求的方法上面加一个@ResponseBody即可。
3. 在你的controller层对应的方法返回html路径及名称时,在前面多加了一个/ 。
例如return “/index”,正式这个/导致报错的,解决:去掉返回前面的/即可,例如return “/index” 。
4. 在使用springboot的过程中,如果使用thymeleaf作为模板文件,则要求HTML格式必须为严格的html5格式,必须有结束标签,否则会报错。
解决方案:1.在application配置文件中配置
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
再在pom.xml 添加以下依赖
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
5. 资源文件的路径被修改,如果你其他的请求都正常返回则可忽略这条。
解决:在pom.xml文件的中加入
<resource>
<directory>src/main/resources</directory>
</resource>