string - 为什么我的 thymeleaf #strings 不起作用?
问题描述
我正在处理的 html 上有一个表格,对于一列,我想显示最多 30 个字符(因为它可能很长)。下面是我的代码,但它不工作,我不知道为什么。任何建议将不胜感激。先感谢您 :)
<span th:text="${#strings.substring(history.keyword,0,30)} + '…'"></span>
解决方案
要使这样的事情起作用,您需要考虑那些可能少于 30 个字符限制的字符串。在这种情况下,上述方法将失败(使用 Java ArrayIndexOutOfBoundsException
)。
有不同的方法。一种方法是使用 Thymeleaf 的“if/else”语法 ( (if) ? (then) : (else)
) 来处理这个问题 - 例如:
<block th:with="kwd=${history.keyword}, len=${#strings.length(history.keyword)}">
<span th:text=" ( ${len > 30} )
? ${#strings.substring(kwd,0,30)} + '…'
: ${kwd}"></span>
</block>
我在上述方法中使用的另一件事是th:with
创建 2 个变量:kwd
并使len
if/else 语句更具可读性。
推荐阅读
- sql - 为什么在 SQL Server 中使用完整的两部分表名
- php - Laravel 6.x 背包 NewsCrud 插件
- sql-server - 如何在 SQL Server 中进行所有数据库备份?
- python-3.x - Python合并两个excel表
- python - Tkinter 和等待
- c# - microsoft face api 错误资源未找到(404)
- c# - .Net Core - 如何从 ResponseStream 或 StreamReader 到实际下载前端的文件?
- c# - 如何使用 c# 配置密码策略(组策略)
- wordpress - 托管问题 - 503 错误
- javascript - 防止 Matter.js 约束中的相对旋转