企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # 内嵌变量 为了了模板更更加易易⽤用,Thymeleaf 还提供了了⼀一系列列 Utility 对象(内置于 Context 中),可以通过 \# 直接访问。 * dates : java.util.Date 的功能⽅方法类 * calendars: 类似 #dates,⾯面向 java.util.Calendar * numbers: 格式化数字的功能⽅方法类 * strings: 字符串串对象的功能类,contains、startWiths、prepending/appending 等 * objects: 对 objects 的功能类操作 * bools: 对布尔值求值的功能⽅方法 * arrays: 对数组的功能类⽅方法 * lists: 对lists 的功能类⽅方法 * sets: set的使用方法 * maps: map的使用方法 # dates 可以使⽤用 dates 对⽇日期格式化,创建当前时间等操作。 ~~~ <!--格式化时间--> <p th:text="${#dates.format(date, 'yyyy-MM-dd HH:mm:ss')}">neo</p> <!--创建当前时间 精确到天--> <p th:text="${#dates.createToday()}">neo</p> <!--创建当前时间 精确到秒--> <p th:text="${#dates.createNow()}">neo</p> ~~~ # strings strings 内置了了⼀一些对字符串串经常使⽤用的函数。 ~~~ <!--判断是否为空--> <p th:text="${#strings.isEmpty(userName)}">userName</p> <!--判断 list 是否为空--> <p th:text="${#strings.listIsEmpty(users)}">userName</p> <!--输出字符串串⻓长度--> <p th:text="${#strings.length(userName)}">userName</p> <!--拼接字符串串--> <p th:text="${#strings.concat(userName,userName,userName)}"></p> <!--创建⾃自定⻓长度的字符串串--> <p th:text="${#strings.randomAlphanumeric(count)}">username</p> ~~~ 后端传值: ~~~ @RequestMapping("/utility") public String utility(ModelMap map) { map.addAttribute("userName", "neo"); map.addAttribute("users", getUserList()); map.addAttribute("count", 12); map.addAttribute("date", new Date()); return "utility"; } ~~~