当前位置:网站首页>Thymeleaf 常用函数
Thymeleaf 常用函数
2022-07-05 14:10:00 【fengyehongWorld】
目录
- 一. #dates
- 二. #numbers
- 三. #strings
- `#strings.toString`
- `#strings.isEmpty`
- `#strings.defaultString`
- `#strings.contains`
- `#strings.containsIgnoreCase`
- `#strings.startsWith`
- `#strings.endsWith`
- `#strings.indexOf`
- `#strings.substring`
- `#strings.replace`
- `#strings.prepend`
- `#strings.append`
- `#strings.toUpperCase`
- `#strings.length`
- `#strings.trim`
- `#strings.abbreviate`
- 四. #objects
- 五. #bools
一. #dates
处理日期数据.生成,转换,获取日期的具体天数和年数。
#dates.createNow
生成当前的日期(相当于Java的new Date())
<span th:text="${#dates.createNow()}"></span>
<!--结果页面-->
<span>Sun May 16 09:38:33 CST 2021</span>
#dates.create
- 构造一个日期
<span th:text="${#dates.create('2019','05','31','10','18')}"></span>
<!--结果页面-->
<span>Fri May 31 10:18:00 CST 2019</span>
#dates.format
- 对日期进行格式化
- 对日期进行格式化的时候,日期一定要是
new Date()类型,不能是字符串日期类型,否则会报错.
<span th:text="*{#dates.format(#dates.createNow())}"></span>
<!--结果页面-->
<span>2021年5月16日 上午09时40分46秒</span>
- 按照指定格式对日期进行格式化
<span th:text="*{#dates.format(#dates.createNow(), 'yyyy/MM/dd HH:mm')}"></span>
<!--结果页面-->
<span>2021/05/16 09:43</span>
<span th:text="*{#dates.format(#dates.createNow(), 'yyyy年MM月dd日 HH:mm')}"></span>
<!--结果页面-->
<span>2021年05月16日 09:43</span>
#dates.year
- 获取年
<span th:text="*{#dates.year(#dates.createNow())}"></span>
<!--结果页面-->
<span>2021</span>
#dates.month
- 获取月份(使用内联的方式)
<span>[[*{#dates.month(#dates.createNow())}]]</span>
<!--结果页面-->
<span>5</span>
#dates.monthName
<span th:text="*{#dates.monthName(#dates.createNow())}"></span>
<!--结果页面-->
<span>五月</span>
#dates.monthNameShort
<span th:text="*{#dates.monthNameShort(#dates.createNow())}"></span>
<!--结果页面-->
<span>五月</span>
#dates.day
- 获取日期
<span th:text="*{#dates.day(#dates.createNow())}"></span>
<!--结果页面-->
<span>30</span>
#dates.dayOfWeek
获取本周的第几天(外国的情况,周日是本周的第一天)
<span th:text="*{#dates.dayOfWeek(#dates.createNow())}"></span>
<!--结果页面-->
<span>1</span>
#dates.dayOfWeekName
<span th:text="*{#dates.dayOfWeekName(#dates.createNow())}"></span>
<!--结果页面-->
<span>星期日</span>
#dates.dayOfWeekNameShort
<span th:text="*{#dates.dayOfWeekNameShort(#dates.createNow())}"></span>
<!--结果页面-->
<span>星期日</span>
#dates.hour
- 获取当前的小时
<span th:text="*{#dates.hour(#dates.createNow())}"></span>
<!--结果页面-->
<span>10</span>
#dates.minute
- 获取当前的分钟
<span th:text="*{#dates.minute(#dates.createNow())}"></span>
<!--结果页面-->
<span>6</span>
#dates.second
- 获取当前的秒
<span th:text="*{#dates.second(#dates.createNow())}"></span>
<!--结果页面-->
<span>12</span>
#dates.millisecond
- 获取当前的毫秒数
<span th:text="*{#dates.millisecond(#dates.createNow())}"></span>
<!--结果页面-->
<span>221</span>
二. #numbers
处理数字数据的转换
- 对不够位数的数字进行补0(formatInteger)
- 设置千位分隔符(formatInteger)
- 精确小数点(formatDecimal)
- 设置百分号(formatPercent)
- 生成数组(sequence)
#numbers.formatInteger
- 对不够位数的数字进行补0
<p th:text="${#numbers.formatInteger('123',4)}"></p> // 0123
<p th:text="${#numbers.formatInteger('123',2)}"></p> // 123
- 设置千位分隔符
.
<p th:text="${#numbers.formatInteger('1000',2,'POINT')}"></p> // 1.000
<p th:text="${#numbers.formatInteger('1000',6,'POINT')}"></p> // 001.000
<p th:text="${#numbers.formatInteger('1000',7,'POINT')}"></p> // 0.001.000
设置千位分隔符,
<p th:text="${#numbers.formatInteger('1000', 2, 'COMMA')}"></p> // 1,000
<div th:text="*{#numbers.formatInteger('1000', 0, 'COMMA')}"></div> // 1,000
设置千位分隔符为空白
<p th:text="${#numbers.formatInteger('1000', 2, 'WHITESPACE')}"></p> // 1 000
#numbers.formatDecimal
精确小数点
<p th:text="${#numbers.formatDecimal('10.123', 3, 2)}"></p> // 010.12
#numbers.formatCurrency
钱显示符号
<p th:text="${#numbers.formatCurrency('1000')}"></p> // ¥1,000.00
#numbers.formatPercent
百分比操作
<p th:text="${#numbers.formatPercent('0.2',2, 4)}"></p> // 20.0000%
<p th:text="${#numbers.formatPercent('0.2',3, 2)}"></p> // 020.00%
三. #strings
- 字符串转换(toString)
- 检查字符串是否为空(isEmpty)
- 字符串是为空替换操作(defaultString)
- 检查字符串中是否包含某个字符串(contains containsIgnoreCase)
- 检查字符串是以片段开头还是结尾(startsWith endsWith)
- 截取(substring substringAfter)
- 替换(replace)
- 追加(prepend append)
- 变更大小写(toUpperCase toLowerCase)
- 拆分和组合字符串(arrayJoin arraySplit)
- 去空格(trim)
- 缩写文本(abbreviate)
- 字符串连接(concat)
#strings.toString
// java代码
Object object = "123";
<p th:text="${#strings.toString(object)}"></p>
<p>123</p>
#strings.isEmpty
String name = null;
<p th:text="${#strings.isEmpty(name)}"></p>
<p>true</p>
#strings.defaultString
String text = null;
String text1 = "123";
<p th:text="${#strings.defaultString(text,'该值为null')}"></p>
<p>该值为null</p>
<p th:text="${#strings.defaultString(text1,'该值为null')}"></p>
<p>123</p>
#strings.contains
<p th:text="${#strings.contains('abcez','ez')}"></p>
<p>true</p>
#strings.containsIgnoreCase
<p th:text="${#strings.containsIgnoreCase('abcEZ','ez')}"></p>
<p>true</p>
#strings.startsWith
<p th:text="${#strings.startsWith('Donabcez','Don')}"></p>
<p>true</p>
#strings.endsWith
<p th:text="${#strings.endsWith('Donabcezn','n')}"></p>
<p>true</p>
#strings.indexOf
<p th:text="${#strings.indexOf('abcefg','e')}"></p>
<p>3</p>
#strings.substring
<p th:text="${#strings.substring('abcefg',3,5)}"></p>
<p>ef</p>
#strings.replace
<p th:text="${#strings.replace('lasabce','las','ler')}"></p>
<p>lerabce</p>
#strings.prepend
<p th:text="${#strings.prepend('abc','012')}"></p>
<p>012abc</p>
#strings.append
<p th:text="${#strings.append('abc','456')}"></p>
<p>abc456</p>
#strings.toUpperCase
<p th:text="${#strings.toLowerCase('ABC')}"></p>
<p>abc</p>
#strings.length
<p th:text="${#strings.length('abc')}"></p>
<p>3</p>
#strings.trim
<p th:text="${#strings.trim(' abc ')}"></p>
<p>abc</p>
#strings.abbreviate
<p th:text="${#strings.abbreviate('12345678910',10)}"></p>
<p>1234567...</p>
四. #objects
#objects.nullSafe
<p th:text="${#objects.nullSafe(null,'该对象为null')}"></p>
<p>该对象为null</p>
五. #bools
#bools.isTrue
<p th:text="${#bools.isTrue(true)} "></p>
<p>true</p>
<p th:text="${#bools.isTrue(false)} "></p>
<p>false</p>
<p th:text="${#bools.isTrue('on')} "></p>
<p>true</p>
<p th:text="${#bools.isTrue('off')} "></p>
<p>false</p>
<p th:text="${#bools.isTrue('true')} "></p>
<p>true</p>
<p th:text="${#bools.isTrue('false')} "></p>
<p>false</p>
<p th:text="${#bools.isTrue(1)} "></p>
<p>true</p>
<p th:text="${#bools.isTrue(0)} "></p>
<p>false</p>
边栏推荐
- 2022 machine fitter (Advanced) test question simulation test question bank simulation test platform operation
- PHP5下WSDL,SOAP调用实现过程
- Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
- -Web direction attack and defense world
- 世界环境日 | 周大福用心服务推动减碳环保
- 展现强大。这样手机就不会难前进
- Webrtc learning (II)
- Scenario based technology architecture process based on tidb - Theory
- R語言ggplot2可視化:可視化折線圖、使用theme函數中的legend.position參數自定義圖例的比特置
- R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用coef函数获取模型中每个变量(自变量改变一个单位)对应的对数优势比(log odds ratio)
猜你喜欢

Recommendation number | what are interesting people looking at?

Shen Ziyu, nouveau Président de Meizu: M. Huang Zhang, fondateur de Meizu, agira comme conseiller stratégique pour les produits scientifiques et technologiques de Meizu

openGauss数据库源码解析系列文章—— 密态等值查询技术详解(下)

循环不变式

LeetCode_ 2 (add two numbers)

Brief introduction to revolutionary neural networks

Qingda KeYue rushes to the science and Innovation Board: the annual revenue is 200million, and it is proposed to raise 750million

TiFlash 面向编译器的自动向量化加速

软件测试人在深圳有哪些值得去的互联网公司【软件测试人员专供版】

What category does the Internet of things application technology major belong to
随机推荐
非技术部门,如何参与 DevOps?
R language ggplot2 visual bar graph: visualize the bar graph through the two-color gradient color theme, and add label text for each bar (geom_text function)
LeetCode_ 67 (binary sum)
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
How to introduce devsecops into enterprises?
Mingfeng medical sprint technology innovation board: annual revenue of 350million yuan, proposed to raise 624million yuan
R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义X轴的轴标签文本(customize X axis labels)
金融壹賬通香港上市:市值63億港元 葉望春稱守正篤實,久久為功
昆仑太科冲刺科创板:年营收1.3亿拟募资5亿 电科太极持股40%
Redis如何实现多可用区?
Make the seckill Carnival more leisurely: the database behind the promotion (Part 2)
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
OSI and tcp/ip protocol cluster
别不服气。手机功能升级就是强
鸿蒙第四次培训
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
Interpretation of tiflash source code (IV) | design and implementation analysis of tiflash DDL module
The forked VM terminated without saying properly goodbye
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
PHP5下WSDL,SOAP调用实现过程