2
2

More than 3 years have passed since last update.

thymeleaf 〇文字目~ 省略表示方法

Last updated at Posted at 2020-04-16

thymeleafでDBから取得した文字列を指定文字数まで表示

今回業務で、ある範囲内に指定文字数を表示し、それ以降は「...」って表示することがあったので、
メモとして記載します。
まだまだ未熟ですので、間違っている箇所等あれば、指摘して頂けると嬉しいです。

・文字列の長さ

"${#strings.length(hoge.str)}" //hoge.strの文字列を取得できる。

・実際の使い方

//10文字以上の場合、9文字目まで表示させて、それ以降は「...」と表示
<span th:if="${10 <= #strings.length(hoge.str)}" th:text="|${#strings.substring(hoge.str,0,9)}...|"></span>
//それ以外は普通に表示
<span th:text="|${#strings.substring(0,9)}...|"></span>

・その他

isEmpty

<th:block th:unless="${#strings.isEmpty(hoge.str)}">
  <span th:if="${10 <= #strings.length(hoge.str)}" th:text="|${#strings.substring(hoge.str,0,9)}...|">/span>
  <span th:text="|${#strings.substring(0,9)}...|"></span>
</th:block>
    hoge.strが空nullでない場合に中の処理を表示

・参考文献
Thymeleafのユーティリティメソッド活用サンプルコード

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2