一、问题症状:404
Whitelabel Error Page
This application has no explicit mapping for /error...
Wed Mar 16 11:20:04 GMT+08:00 2022
There was an unexpected error (type=Not Found, status=404).
No message available
设置了首页,后台也报警告:
2022-03-16 11:23:10.782 WARN 7556 --- [nio-8089-exec-1] o.s.w.s.r.ResourceHttpRequestHandler : "Path represents URL or has "url:" prefix: [classpath:/templates/index.html]"
本次原因及解决办法:
不知道当时从那拷贝过来一堆配置,里面多了几句话
spring:
thymeleaf:
cache: false
#......#等等其他配置
mode: LEGACYHTML5
template-resolver-order: ''
view-names: ''
其中,
view-names: '' #可以解析的视图名称的逗号分隔列表
也就是允许解析和访问的模板列表,,
不要瞎加配置,也瞎复制配置,,,,坑爹 404,,
查询的步骤:
- 新建一个空Springboot thymeleaf 项目,配置全去掉,正常
- 加上配置,异常 404
- 二分法,逐块删除配置,,剩的少了,逐行删除配置,找到该配置项
======================================
二、问题症状:template might not exist 模板不存在
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Mar 16 11:43:07 GMT+08:00 2022
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
后台报错,
2022-03-16 11:43:07.254 ERROR 4704 --- [nio-8089-exec-1] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8089-exec-1] Exception processing template "index": Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index], template might not exist or might not be accessible by any of the configured Template Resolvers
at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:869) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
其实就是模板不存在,
先看下 target的目录先究竟有没有 templates文件夹 ,
没有的话,加上如下内容。(暂时不明白,为啥有的不加也可以)
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes><!--这是mybatis plus 的东西,和上面错无关-->
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory><!--资源文件的路径-->
<includes>
<include>**/*.*</include>
</includes>
<!-- <filtering>false</filtering>-->
</resource>
</resources>
....
....
</build>
=======================================
三、低版本thymeleaf,严格的html5格式
低版本thymeleaf 要求HTML格式必须为严格的html5格式,必须有结束标签,格式要求也较严格
解决办法:升级版本吧,或者费劲的如下
thymeleaf:
mode: LEGACYHTML5
并且 pom.xml 添加nekohtml依赖,
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
不过,我用的高版本,高版本好像没事,暂时不入坑。