前言
上一篇我们讲了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源码分析,等等等,同时还会分享一些赚钱理财的小套路哦,欢迎大家来支持,一起学习成长,程序员不仅仅是搬瓦工!
公众号:分享系列好文章
交流群:群友互相分享资料