SpringBoot use Thymeleaf module can not access static pages
Recently used in the process of learning SpringBoot Thymeleaf engine module, always send a request after the page can not display static pages, all the steps are references to perform a self-examination for a long time did not answer to your question, whoops, my violent temper on the table, a small page I wanted stumped? I also how to find ONE PIECE?
Here I give share my tragic mentality:
To use Thymeleaf module engine, we first introduced its dependencies pom file as follows:
We do not need to specify the version here, because SpringBoot will default spring-boot-starter-parent to us for the management of project dependencies version, and here I refer to the manual that specifies the version Thymeleaf module engine 3.0, here we note, since I defined Thymeleaf version also specifies the version thymeleaf-layout-dialect when using Thymeleaf3 as the main program, version of our thymeleaf-layout-dialect layout features, if 2 or more, when used as a main Thymeleaf2, our thymeleaf- layout-dialect version of the layout function, if 1 or more. As for specific reasons, we can see thymeleaf-layout-dialect can be seen on GitHub:
Thymeleaf-layout-dialect layout features version 2 or later is rewritten to support Thymeleaf 3's.
How to use, I made reference to the use of official documents:
Thymeleaf module defines its own version of the engine 3.
Then I wrote a simple controller to verify whether success:
At this time the accident occurred:
原来是我写的RequestMapping中指定的路径和返回的视图名冲突了,修改一下视图名或者路径就可以了,这个也不是什么高深的问题,只不过新手的话,比较容易疏忽,这一块还是要避免的。修改后我们看下结果,果然,福无双至,祸不单行,运行后结果如下:
黑人问号???
出现No message available这种问题,一般这种情况,可能是URL错误或者启动类没有扫描到,检查一下路径和启动类,没毛病,我还在controller里面加上了在控制台打印的话,运行后也是能接受到请求的,但是就是返回不了页面的内容,一切都是这么合理,能想到的就是版本问题了,果然,网上也有人指出来是版本兼容问题,废话不多说,改版本:
我把Thymeleaf的版本号改成最新的3.0.11,执行后,果然奏效:
不过问题虽然解决了,但是这个版本号是从官方手册上看到了,于是倔强的我把Thymeleaf 3.0.2~3.0.11所有的版本都试了一遍(是个狼人??),最后发现,3.0.2~3.0.7,项目启动正常,但是无法返回页面,3.0.8~3.0.9,项目启动时会报出一些提示信息:
3.0.10~3.0.11,项目正常启动,且可以返回页面信息。
最后找了一下官网的手册,发现只有1.5.22版本的SpringBoot有使用版本的推荐(可惜使用下来发现不能用),2.1.7版本的手册上并没有写,不过查看了一下2.1.7版本的spring-boot-starter-parent,发现SpringBoot中默认使用的Thymeleaf版本和布局功能的版本都挺新的,我们使用过程中可以不用自己制定版本,用默认的就行了:
以上就是我解决这个问题的过程,大概原因就是版本兼容问题,我们可以使用SpringBoot默认的版本配置,或者使用我如上尝试成功地配置版本,至于深层次原因,暂时还没有找到比较权威的资料来证明,如果有哪位巨佬了解,还望不吝赐教。