简单了解Spring中的freemaker 和 thymeleaf

Spring中的freemaker 和 thymeleaf
freemaker
  1. 添加依赖

    <dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-freemarker</artifactId>
    </dependency>
    
  2. 添加配置文件

    # freemarker配置
    #===========================
    # 是否开启thymeleaf缓存,本地为false,⽣生产建议为true
    spring.freemarker.cache=false
    spring.freemarker.charset=UTF-8
    spring.freemarker.allow-request-override=false
    spring.freemarker.check-template-location=true
    #类型
    spring.freemarker.content-type=text/html
    spring.freemarker.expose-request-attributes=true
    spring.freemarker.expose-session-attributes=true
    #⽂文件后缀
    spring.freemarker.suffix=.ftl
    #路路径
    spring.freemarker.template-loader-path=classpath:/templates/
    
    
  3. 取值得命名规则

    @GetMapping("freemaker")
    public String index(ModelMap modelMap) {
           modelMap.addAttribute("setting", config);
           // 不需要加后缀
           // 因为配置文件已经指定了后缀
           // 返回得页面路径
           return "user/fm/index";
    }
    
    // ftl页面中获取属性
    <h1>${setting.payAppid}</h1>
    
thymeleaf
  1. 添加依赖

    <dependency>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    
  2. 添加配置文件

    # thymeleaf配置
    #==================================
    #开发时关闭缓存,不不然没法看到实时⻚页⾯面
    spring.thymeleaf.cache=false
    spring.thymeleaf.mode=HTML5
    #前缀
    spring.thymeleaf.prefix=classpath:/templates/
    #编码
    spring.thymeleaf.encoding=UTF-8
    #类型
    spring.thymeleaf.content-type=text/html
    #名称的后缀
    spring.thymeleaf.suffix=.html
    
  3. 取值命名规则

    @GetMapping("thymeleaf")
    public String index2(ModelMap modelMap) {
          modelMap.addAttribute("setting", config)
          // 不需要加后缀
          // 因为配置文件已经指定了后缀
          return "tl/index";
    }
    
    // html 页面中获取得属性
    // 在开头的html中得添加
    <html xmlns:th="http://www/thymeleaf.org">
    // 如何去获取
    <p th:text="${setting.payAppid}"></p>
    <p th:text="${setting.paySecret}"></p>
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值