【spring boot2】第5篇:spring boot对模板引擎的支持

spring boot中支持哪些模板引擎

spring boot 整合 freemarker

添加场景启动器

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

spring boot 如何配置 freemarker

  • FreeMarkerAutoConfiguration :自动配置,给容器中添加 freemarker 相关组件
  • FreeMarkerProperties :配置 freemarker 的相关属性
@ConfigurationProperties(prefix = "spring.freemarker")
public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties {
    //模板文件存放的路径,存放在该默认路径的文件 freemarker会自动渲染
    public static final String DEFAULT_TEMPLATE_LOADER_PATH = "classpath:/templates/";

    public static final String DEFAULT_PREFIX = "";
    //模板文件默认后缀,可以在属性文件中配置覆盖
    public static final String DEFAULT_SUFFIX = ".ftl";
}

freemarker 语法介绍

spring boot 整合 thymeleaf

pom.xml中添加依赖

 <!--配置启动器-->
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring‐boot‐starter‐thymeleaf</artifactId>
</dependency>

<!--修改thymeleaf的版本号-->
properties>
    <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    <!‐‐ 布局功能的支持程序 thymeleaf3 则需要 layout2 以上版本 ‐‐>
    <thymeleaf‐layout‐dialect.version>2.2.2</thymeleaf‐layout‐  dialect.version>
</properties>

thymeleaf的属性配置类

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

    private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

    public static final String DEFAULT_PREFIX = "classpath:/templates/";

    public static final String DEFAULT_SUFFIX = ".html";
}
  • utf-8 编码文件
  • 使用html文件
  • 把html页面放在classpath:/templates/中就能自动渲染

thymeleaf 语法

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值