springboot入门系列教程|第五篇:thymeleaf使用详解之表达式对象以及常用配置

版权声明:转载请注明地址,谢谢配合 https://blog.csdn.net/pulong0748/article/details/82391660

前言

     上一篇我们讲了thymeleaf的常用属性以及常用字面量,最后我们再介绍下thymeleaf的内置对象。

thymealeaf表达式对象

  • #request和#session
          后端代码:
 @RequestMapping(value = "/request")
    public String request(Model model,  HttpServletRequest request){
        request.setAttribute("wxCode","codeStone");
        request.getSession().setAttribute("name","Tom");
        return "index";
    }

     前端代码:

     我的微信公众号:
<span th:text="${#request.getAttribute('wxCode')}"></span><br>
项目上下文:
<span th:text="${#request.getContextPath()}"></span><br>
我的名字:
<span th:text="${#request.getSession().getAttribute('name')}"></span>

     结果展示:
这里写图片描述


表达式功能对象

     后端代码:

model.addAttribute("date",new Date());

     前端代码:

<span th:text="${#dates.format(date,'YYYY-MM-dd HH:mm:ss')}">时间输出</span>

     展示:
这里写图片描述

     还有一些常用的表达式对象就不一一介绍了,大致的罗列一下,自己一定要学会看官方文档

#strings: 字符串对象的实用方法: 
contains, startsWith, prepending/appending等;

#objects: 对objects操作的实用方法;

#bools: 对布尔值求值的实用方法;

#arrays: 数组的实用方法;

#sets: set的实用方法;

#maps: map的实用方法;

     最后再来看一下thymeleaf的常用配置:

#启用模板缓存
spring.thymeleaf.cache = true 

#检查模板位置是否存在。
spring.thymeleaf.check-template-location = true

#Content-Type值
#spring.thymeleaf.content-type = text/html 

#启用MVC Thymeleaf视图解析
spring.thymeleaf.enabled = true 

#模板编码
spring.thymeleaf.encoding = UTF-8 

#设置前缀
spring.thymeleaf.prefix = /templates/

#设置后缀
#spring.thymeleaf.suffix = .html 

     总结一下:我这几天写的关于thymeleaf的文章:
     springboot入门系列教程|第二篇:springboot 结合thymeleaf 模板引擎初探
     springboot入门系列教程|第三篇:thymeleaf使用详解之标准表达式
     springboot入门系列教程|第四篇:thymeleaf使用详解之常见属性以及常用字面量

     获取本章源码:springBoot-study-thymeleaf
     希望大家支持一下我的微信公众号:coldStone,主要会分享分布式系统相关的一系列技术,目前会推出springboot、springcloud和docker系列教程,后期会有关于中间件以及各个层面的性能优化的文章,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
这里写图片描述

猜你喜欢

转载自blog.csdn.net/pulong0748/article/details/82391660
今日推荐