问题说明
在我的意识中,是主导使用thymeleaf的,因为我一直想知道springboot推荐的它到底有什么好。在普通的springboot项目中,直接引入thymeleaf的依赖包就可以进行视图解析了,而在一般的spring项目中还需要我们进行一些手动的配置,于是就出现了这个问题,怎么整合多个模板引擎在一起。
解决方案
- 引入thymeleaf的依赖
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring3 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
thymeleaf的版本2和3都行,3功能更完善一点。
另外tymeleaf-spring3 or thymeleaf-spring4也随意使用。
2. 配置springmvc视图解析
关键是这一步,这一步网上的版本就多了。我大概分析了一下主要有下面两种情况:
- 第一种情况jsp页面和html页面分别是位于不同的视图文件夹下面
- 第二种情况两种页面同时存在同一个视图文件夹中
先来看第一种情况的配置:
这里可以参考thymeleaf官方的一个宠物商店的配置地址是
thymeleaf官方配置例子
下面是咱们的例子:
<bean id="templateResolver"
class="org.thymeleaf.spring3.templateresolver.SpringResourceTemplateResolver">
<property name="prefix&#