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

前言

     上一篇我们讲了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


如果有小伙伴觉得我写的不错的话可以关注一下我的博客,我会一直持续更新,也可以支持一下我的公众号哦:java架构师小密圈,会分享架构师所必须深入研究的技术,比如netty,分布式,性能优化,spring源码分析,mybatis源码分析,等等等,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
公众号:分享系列好文章
java架构师小密圈

交流群:群友互相分享资料
java架构师小密圈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mindcarver

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值