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

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog
IT之家
IT之家
博客园 - 聂微东
The Cloudflare Blog
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
H
Help Net Security
博客园 - 叶小钗
V
V2EX
WordPress大学
WordPress大学
J
Java Code Geeks
Hugging Face - Blog
Hugging Face - Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
C
Check Point Blog
B
Blog
D
DataBreaches.Net
美团技术团队
罗磊的独立博客

皓子的小站

网站字体应用之坑——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) 不要与蠢人辩论 小心网络地雷·续篇 当心网络组织“开往”·续篇 当心网络组织“开往”
在 Halo CMS 中通过模&#26495...
HowieHz · 2025-03-30 · via 皓子的小站

这篇文章 Thymeleaf 随机数生成与格式化详解(整数/小数/浮点数) 的一个应用示例。

纯 Thymeleaf 模板实现

<a
  th:with="allPostList=${postFinder.listAll()},
    randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(allPostList))},
    postPermalink=${allPostList[randomIndex].status.permalink}"
  th:href="${postPermalink}"
>随机文章</a>

结合 Javascript 使用

插入以下代码到页面中,之后调用 toRandomPost() 即可跳转到随机博文。

<script
  th:inline="javascript"
  th:with="allPostList=${postFinder.listAll()},
    randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(allPostList))},
    postPermalink=${allPostList[randomIndex].status.permalink}"
>
    function toRandomPost() {
        let permalink = /*[[${postPermalink}]]*/ "/";
        
        // 选择任意方法跳转
        // window.open(permalink);
        // pjax.loadUrl(permalink);
        window.location.href = permalink;
    }
</script>

省略中间变量也是可以的

<script
  th:inline="javascript"
  th:with="allPostList=${postFinder.listAll()},
    randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(allPostList))},
    postPermalink=${allPostList[randomIndex].status.permalink}"
>
    function toRandomPost() {
        // 选择任意方法跳转
        // window.location.href = /*[[${postPermalink}]]*/ "/";
        // pjax.loadUrl(/*[[${postPermalink}]]*/ "/");
        window.open(/*[[${postPermalink}]]*/ "/");
    }
</script>

拓展

在上面代码中 allPostList[randomIndex] 返回的是 ListedPostVo 类型的变量。
借此你可以拿到文章更多相关信息,如:标题,创建时间,发布时间,是否置顶,摘要内容等。
以下的两个示例添加了获取文章标题的部分。

<a
  th:with="allPostList=${postFinder.listAll()},
    randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(allPostList))},
    postPermalink=${allPostList[randomIndex].status.permalink},
    postTitle=${allPostList[randomIndex].spec.title}"
  th:href="${postPermalink}"
  th:text="${postTitle}"
></a>
<script
  th:inline="javascript"
  th:with="allPostList=${postFinder.listAll()},
    randomIndex=${T(java.lang.Math).floor(T(java.lang.Math).random()*#lists.size(allPostList))},
    postPermalink=${allPostList[randomIndex].status.permalink},
    postTitle=${allPostList[randomIndex].spec.title}"
>
    <!--/* 小技巧:写在此处的注释在模板渲染后会去掉,可减小页面体积 */-->
    function toRandomPost() {
        // <!--/* 弹出提示框 显示文章标题 */-->
        alert(/*[[${postTitle}]]*/ "");
        
        // <!--/* 选择任意方法打开链接 */-->
        // window.open(/*[[${postPermalink}]]*/ "/");
        // pjax.loadUrl(/*[[${postPermalink}]]*/ "/");
        window.location.href = /*[[${postPermalink}]]*/ "/";
    }
</script>

实现示例

结语

欢迎留言支持/交流