34
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Thymeleafのユーティリティメソッド活用サンプルコード

Posted at

リストや集約の使い方を調べたので、メモがてらに投稿しておく。

集約 #aggregates

sum

<div class="totalPrice" th:text="|${#aggregates.sum(productList.![price])}円(+税)|">
    1110円(+税)
    productListのprice項目を合計する ※springの場合の記法!
    Thymeleaf チュートリアルではxxx.{yyy * zzz}の形式で書かれている
</div>

文字列 #strings

listJoin

<div class="Contents" th:text="${#strings.listJoin(productList.![id],',')}">
    productListのid項目を「,」で区切って並べる
    例) productList[x].id -> 「100」「150」「200」
        text="100,150,200"
</div>

isEmpty

<th:block th:if="${#strings.isEmpty(str)}">
    strが空、nullの場合にtrueになる。事前にtrim()されるので空白もtrue。
    th:blockは、thymeleafの処理だけしたい時に便利なタグ
</th:block>

リスト #lists

isEmpty

<div class="Contents" th:if="${#lists.isEmpty(productList)}">
    productListオブジェクトが空の場合に有効
</div>
<div class="Contents" th:if="!${#lists.isEmpty(productList)}">
    productListオブジェクトが空じゃない場合に有効
</div>

size

<th:block th:if="${#lists.size(productList)}>1" >
    productListオブジェクトが1個より多い時だけ有効
    
</th:block>
34
39
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
34
39

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?