thymeleaf strings substring 报错解决办法

分享 未结 0 1134
邮箱用户_pb5l1
邮箱用户_pb5l1 LV1 2021年7月2日 23:23 编辑

thymeleaf strings substring 报错解决办法

${#strings.substring(data.title, 0, 30)}

在使用thymeleaf 字符串截取时报错

java.lang.StringIndexOutOfBoundsException: begin 0, end 30, length 25

所以在使用前需要判断下长度

${#strings.length(data.title) > 30 ? #strings.substring(data.title, 0, 30) : data.title}

具体程序中使用写法

<a class="jie-title" th:href="@{'/article/'+ ${question.id}}" th:text="${#strings.length(question.title) > 80 ? #strings.substring(question.title, 0, 80) : question.title}" target="_blank"></a>


收藏(0)  分享
相关标签: 笔记
问题没解决?让chatGPT帮你作答 智能助手
0个回复
  • 消灭零回复