Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
30
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@55beagle

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

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

集約 #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>
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
30
Help us understand the problem. What are the problem?