0
点赞
收藏
分享

微信扫一扫

SpringBoot_整合视图层技术

山竹山竹px 2022-01-08 阅读 53

SpringBoot_整合视图层技术

一、整合Thymeleaf

Thymeleaf是新一代Java模板引擎,类似于Velocity、FreeMarker等传统Java模板引擎。与传统Java模板引擎不同的是,Thymeleaf支持HTML原型。SpringBoot提供了Thymeleaf自动化配置解决方案,因此在SpringBoot中使用Thymeleaf非常方便。SpringBoot整合Thymeleaf主要可通过如下步骤:

  1. 创建工程,添加依赖
        <!--整合Thymeleaf-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  1. 配置Thymeleaf
    SpringBoot为Thymeleaf提供了自动配置类ThymeleafAutoConfiguration,相关的配置属性在ThymeleafProperties类中,ThymeleafProperties部分源码如下:
    在这里插入图片描述
spring:
  thymeleaf:
    cache: true   #是否开启缓存
    check-template: true #检查模板是否存在
    check-template-location: true #检查模板位置是否存在
    encoding: utf-8 #模板文件编码
    prefix: classpath:/templates/ #模板文件位置
    suffix: .html #模板文件后缀
    servlet:
      content-type: text/html   #Content-type配置

  1. 配置控制器

在这里插入图片描述
5. 创建视图
在这里插入图片描述

导入Thymeleaf的名称空间,更多用法参考https://www.thymeleaf.orf

二、整合FreeMarker

FreeMarker是一个非常古老的模板引擎,可以用在Web环境或者非Web环境中。与Thymeleaf不同,FreeMarker需要经过解析才能够在浏览器展示出来。FreeMarker不仅可以用来配置HTML页面模板,也可以作为电子邮件模板、配置文件模板以及源码模板等。

  1. 创建项目,添加依赖
<!--整合FreeMarker-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
  1. 配置FreeMarker

  2. 配置控制器
    在这里插入图片描述

  3. 创建视图 在这里插入图片描述
    关于FreeMarker更多资料:https://freemarker.apache.org/

举报

相关推荐

0 条评论