在使用SpringBoot的thymeleaf时,向往常一样编写了<a href="xxx">xxx</a>的超链接。

结果404错误爆炸。还以为自己链接写错了,改得我差点怀疑人生。

度娘了一下发现是SpringBoot本身的配置问题。

在对application.yml文件进行如下配置后,顺利解决了无法访问静态资源的问题。

mvc:
static-path-pattern: /**

resources:
static-locations: classpath:/static/,classpath:/public/,classpath:/templates/,classpath:/resources/,classpath:/META-INF/resources/

顺带记录一下SpringBoot经典的标签闭合错误问题。默认配置的SpringBoot使用Thymeleaf渲染页面的模式是HTML5

在该模式中Thymeleaf会严格遵循HTML5标准,非闭合标签会导致页面渲染失败。(比如用IDEA新建的HTML页面的Mate标签就没有闭合)。

解决这个问题需要引入第三方工具并修改Thymeleaf的渲染模式。

thymeleaf:
mode: LEGACYHTML5

pom.xml 添加下面的依赖

<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>