惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
M
MIT News - Artificial intelligence
量子位
N
Netflix TechBlog - Medium
The Cloudflare Blog
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
B
Blog
博客园_首页
博客园 - Franky
MyScale Blog
MyScale Blog
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
MongoDB | Blog
MongoDB | Blog
云风的 BLOG
云风的 BLOG
爱范儿
爱范儿
H
Help Net Security
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
酷 壳 – CoolShell
酷 壳 – CoolShell

皓子的小站

网站字体应用之坑——font-family 篇 糟糕的 meta name="theme-color" How to Automatically Track Newly Supported ESLint Rules in Oxlint How to Gradually Migrate from ESLint to Oxlint (Without Breaking Everything) 静态资源预压缩:零运行时开销,极致节省带宽 CVE-2025-70886 Proof of Concept (PoC) | A Script to Crash Halo CMS Comment Backend 自定义网页鼠标指针——一段曲折的旅程 CVE-2025-70886 漏洞复现/PoC | 一个脚本让 Halo CMS 评论后台瘫痪 2025 年终总结 & 博客两周年 老用户专享!已有 Halo 专业版授权可免费升级商业版 自动追踪 Oxlint 对 ESLint 规则的新增支持 Halo 贡献者证书与实体周边盲盒开箱 博客评论系统指南 CDN 回源跟随配置导致登录异常问题排查 SCDN 免费赞助计划:助力高质量博客&博客圈 锐捷校园网:网络共享与带宽叠加方案(哈理工案例) ESLint 到 Oxlint 渐进式迁移快速上手指南 Fixing Vite Breaking Inline JS & CSS in Thymeleaf Templates 解决 Vite 破坏 Thymeleaf 模板内联 JS & CSS 的方法 我的博客,为什么是月更? 博客俱乐部一周年纪念品开箱 网站字体加载之坑——format 篇 经历 1000000000 次 DDoS 请求攻击后,我总结了三条经验 题解分享:[AtCoder Beginner Contest 414 E] Count A%B=C 题解分享:[蓝桥杯 2025 国 Python A] 杨辉三角 P12876 FiF口语训练破解刷分教程(适用于 Windows) 不要与蠢人辩论 小心网络地雷·续篇 当心网络组织“开往”·续篇 当心网络组织“开往”
Thymeleaf 随机数生成&#19982...
HowieHz · 2025-02-19 · via 皓子的小站

本文提出的示例均在 Thymeleaf 3.1 上经过测试。
可适用于多种使用 Thymeleaf 的场景,如 Halo CMS 主题获取随机数。

This article is also available in: English
此文章有英文版本

整数

生成范围在 [min, min+range) 中的随机整数

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}

生成范围在 [min, max] 中的随机整数

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}
点我查看示例

一个 p 标签,文本内容为一个范围在 [1, 6)[1, 5]) 的整数,可能的结果有:1, 2, 3, 4, 5

方法一

<p
  th:with="range=5,min=1"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),0,0)}"
></p>

方法二

<p
  th:with="max=5,min=1"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),0,0)}"
></p>

用作列表索引值

用作列表索引值其实不需要 #numbers.formatDecimal。如下示例,将两处 my_list 修改为实际列表,就可以随机显示一个列表中的值。

点我查看示例
<p
  th:with="randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(my_list))}"
  th:text="${my_list[randomIndex]}"
></p>

控制结果形式

生成范围在 [min, min+range) 中的随机整数。结果形式为:整数部分位数大于等于 integerDigits(不足的位数用前导 0 补足),小数部分位数等于 decimalDigits (用 0 补足不够的位数)

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}

生成范围在 [min, max] 中的随机整数。结果形式为:整数部分位数大于等于 integerDigits(不足的位数用前导 0 补足),小数部分位数等于 decimalDigits (用 0 补足不够的位数)

${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}
点我查看示例

一个 p 标签,文本内容为一个范围在 [0, 5)[0, 4]) 的整数,可能的结果有:000.00, 001.00, 002.00, 003.00, 004.00

方法一

<p 
  th:with="range=5,min=0,integerDigits=3,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*range+min),integerDigits,decimalDigits)}"
></p>

方法二

<p 
  th:with="max=4,min=0,integerDigits=3,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).floor(T(java.lang.Math).random()*(max-min+1)+min),integerDigits,decimalDigits)}"
></p>

小数/浮点数

生成范围在 [min, max) 中的随机浮点数

${T(java.lang.Math).random()*(max-min)+min}
点我查看示例

一个 p 标签,文本内容为一个范围在 [0.5, 1.8) 的浮点数,可能的结果有:1.46833041859471749, 0.5578399752996518908, 1.15072203796468388

<p
  th:with="max=1.8,min=0.5"
  th:text="${T(java.lang.Math).random()*(max-min)+min}"
></p>

控制结果形式

生成范围在 [min, max) 中的随机浮点数。结果形式为:整数部分位数大于等于 integerDigits(不足的位数用前导 0 补足),小数部分位数等于 decimalDigits (用 0 补足不够的位数)

${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}
点我查看示例

一个 p 标签,文本内容为一个范围在 [0.5, 150.8) 的浮点数,可能的结果有:00.83, 70.22, 150.23

<p
  th:with="max=150.8,min=0.5,integerDigits=2,decimalDigits=2"
  th:text="${#numbers.formatDecimal(T(java.lang.Math).random()*(max-min)+min,integerDigits,decimalDigits)}"
></p>

原项目相关进展

截至此文发布时(2025.2.19),Thymeleaf 关于随机数的 Issue 依旧无新动态,保持开放状态:
Random integer in #numbers · Issue #787 · thymeleaf/thymeleaf