SpringBoot使用thymeleaf模板引擎

时间:2022-11-17 18:22:18

按照http://blog.csdn.net/u012706811/article/details/52185345里的方法配置thymeleaf,启动时出现下列异常

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'viewResolver' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$Thymeleaf3Configuration$Thymeleaf3ViewResolverConfiguration': Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration$ThymeleafDefaultConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultTemplateResolver' defined in class path resource [org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration$Thymeleaf3Configuration$DefaultTemplateResolverConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver]: Factory method 'defaultTemplateResolver' threw exception; nested exception is java.lang.NoSuchMethodError: org.thymeleaf.templateresolver.TemplateResolver.checkNotInitialized()V

后来在官方文档中看到

By default, spring-boot-starter-thymeleaf uses Thymeleaf 2.1. If you
are using the spring-boot-starter-parent, you can use Thymeleaf 3 by
overriding the thymeleaf.version and thymeleaf-layout-dialect.version
properties, for example:

<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

if you are managing dependencies yourself, look at
spring-boot-dependencies for the list of artifacts that are related to
those two versions. To avoid a warning message about the HTML 5
template mode being deprecated and the HTML template mode being used
instead, you may also want to explicitly configure
spring.thymeleaf.mode to be HTML, for example:

spring.thymeleaf.mode: HTML

Please refer to the Thymeleaf 3 sample to
see this in action.

If you are using any of the other auto-configured Thymeleaf Extras
(Spring Security, Data Attribute, or Java 8 Time) you should also
override each of their versions to one that is compatible with
Thymeleaf 3.0.

意思是可以在maven中配置Thymeleaf 版本,由于不知道什么原因导致的错误,于是尝试了一下改变Thymeleaf 的版本,也就是在pom.xml文件中<properties></properties>标签内添加一下配置

<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>

再次启动果然有效果,因此可以猜测应该是因为spring-boot-starter-thymeleaf默认使用 Thymeleaf 2.1,而当前SpringBoot版本(我的是1.5.2)不支持该版本的Thymeleaf,手动修改为Thymeleaf 3.0.2的版本。
事实上开头的链接的末尾已经写了更改版本号,当时没注意。

PS:经过了n次的报错查了半天找不出解决办法,总结出异常无论如何都解决不了时考虑依赖的版本问题。