Thymeleaf模板是原生的,不依赖于标签库。它能够接受原始HTML进行编辑和渲染。
1. 配置Thymeleaf视图解析器
为了要在Spring中使用Thymeleaf,我们需要配置三个启用Thymeleaf与Spring集成的bean:
ThymeleafViewResolver
:将逻辑视图名称解析为Thymeleaf模板视图SpringTemplateEngine
:处理模板并渲染结果TemplateResolver
:加载Themeleaf模板
<thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
...
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
1.1 使用Java配置
@Bean //Thymeleaf视图解析器
@Autowired
@Qualifier