SpringBoot整合Thymeleaf和FreeMarker模板引擎

 时间:2026-02-17 08:09:32

1、首先我们需要在pom文件中引入Thymeleaf和FreeMarker相关的依赖。

<dependency>


   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>


   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

SpringBoot整合Thymeleaf和FreeMarker模板引擎

2、Thymeleaf是支持HTML的,默认的模板文件的位置在classpath:/templates/,默认的模板后缀名为.html。FreeMarker默认的模板文件的位置在classpath:/templates/,默认的模板后缀名为.ftl。

SpringBoot整合Thymeleaf和FreeMarker模板引擎

3、接下来我们创建Book实体类,添加id, name,author三个属性,并生成相应的getter/setter方法。

SpringBoot整合Thymeleaf和FreeMarker模板引擎

4、然后创建BookController,在Controller中返回ModelAndView。

SpringBoot整合Thymeleaf和FreeMarker模板引擎

5、接下来我们看看Thymeleaf的模板文件books.html的写法,如下所示,用th:each遍历后获取值并展示。接下来我们启动服务,然后请求/books,结果为:

SpringBoot整合Thymeleaf和FreeMarker模板引擎

SpringBoot整合Thymeleaf和FreeMarker模板引擎

6、我们再来看FreeMarker模板文件的写法,如下为books.ftl文件的内容,两个模板引擎可以分开在两个工程中,方便查看效果。

SpringBoot整合Thymeleaf和FreeMarker模板引擎

  • AU中如何设置默认中置声道提取效果?
  • 字幕的透明度怎么调整
  • Premiere 导入文件的方式
  • PotPlayer如何更改内置声音处理滤镜设置
  • PR软件如何制作分屏转场效果
  • 热门搜索
    微信用英语怎么说 cpu风扇怎么拆 微信怎么发起接龙 眉毛怎么修 安全警告怎么关闭 肉夹馍怎么做 偏头疼怎么办 珀莱雅化妆品怎么样 心情不好怎么发泄 月份用英语怎么说