【问题标题】:Spring thymeleaf limit text output in a th:each loopSpring thymeleaf 限制 th:each 循环中的文本输出
【发布时间】:2019-08-05 02:41:25
【问题描述】:

我目前正在做一个学校项目,几乎完成了。我有一个管理面板,我在其中列出了我的博文,我想限制文本字段,这样当它达到 150 个字符时,它会中断并输入 3 个点“...”..

列表的代码在这里:

 <tr th:each = "blog: ${blogs}">
        <th th:text="${blog.title}" scope="row"></th>
        <td><p th:text="${blog.text}"></p></td>
        <td><p th:text="${blog.author}"></p></td>
        <td><p th:text="${blog.date}"></p></td>
        <td><a th:href="@{/blog/delete/{id}(id=${blog.id})}"class="btn btn-danger">Delete</a><a th:href="@{/blog/update/{id}(id=${blog.id})}" class="btn btn-info ml-2">Edit</a></td>
    </tr>

我尝试在谷歌上搜索,但找不到解决方案。

感谢您的帮助!

/尼克

【问题讨论】:

  • 感谢您的解决方案。它工作得很好,但我还是想知道百里香的语法,因为我还想在博文上做一个“阅读更多”按钮,所以如果博文超过 150 个字符,你可以按阅读更多查看完整博文

标签: java spring thymeleaf


【解决方案1】:

如果你不想通过 CSS 来做,你可以这样做:

<td><p th:text="${#strings.length(blog.text)>150 ? #strings.substring(blog.text,0,150) + '...'} : blog.text"></p></td>

编辑: 搜索更多后发现这个简单的方法:

<td><p th:text="${#strings.abbreviate(blog.text,150)} "></p></td>

这是执行上一个解决方案的简写符号。您可以在here 中找到详细信息。

【讨论】:

    猜你喜欢
    • 2018-01-07
    • 2014-06-08
    • 2018-05-09
    • 2013-07-24
    • 1970-01-01
    • 2019-05-07
    • 2018-05-11
    • 2015-09-04
    • 1970-01-01
    相关资源
    最近更新 更多