Thymeleaf

HTML 属性(ぞくせい) 対照(たいしょう) (ひょう)

標準(ひょうじゅん) (しき) 構文(こうぶん) (Standard Expression Syntax)

キーワード説明(せつめい)(れい)
${...}変数(へんすう) (しき) (Variable Expression)${user.name}
*{...}選択(せんたく) (しき) (Selection Expression)*{name}
#{...}メッセージ(しき) (Message Expression)#{welcome.message}
@{...}リンク(しき) (Link Expression)@{/user/profile}
~{...}フラグメント(しき) (Fragment Expression)~{header :: menu}

標準(ひょうじゅん) 属性(ぞくせい) プロセッサ (Standard Attribute Processors)

キーワード説明(せつめい)(れい)
th:text要素(ようそ) のテキスト内容(ないよう)設定(せってい)<p th:text="${user.name}">Default Name</p>
th:utextエスケープなし HTML 内容(ないよう)設定(せってい)<div th:utext="${htmlContent}">Default Content</div>
th:eachコレクションを反復(はんぷく)<tr th:each="user : ${users}">
th:if条件(じょうけん) ()要素(ようそ) 表示(ひょうじ)<div th:if="${not #lists.isEmpty(users)}">
th:unless条件(じょうけん) ()要素(ようそ) 非表示(ひひょうじ)<div th:unless="${#lists.isEmpty(users)}">
th:switch/th:case複数(ふくすう) 条件(じょうけん) 判定(はんてい)<div th:switch="${user.role}"><p th:case="'admin'">Admin</p></div>
th:attr任意(にんい)属性(ぞくせい)設定(せってい)<img th:attr="src=@{/images/logo.png},title=${logoTitle}">
th:value要素(ようそ) の Value ()設定(せってい)<input th:value="${user.name}">
th:actionフォーム送信先(そうしんさき)設定(せってい)<form th:action="@{/user/save}">
th:hrefリンク URL を設定(せってい)<a th:href="@{/user/{id}(id=${user.id})}">
th:srcメディアリソースの参照元(さんしょうもと)設定(せってい)<img th:src="@{/images/logo.png}">
th:withローカル変数(へんすう)定義(ていぎ)<div th:with="isAdmin=${user.role == 'admin'}">

フォーム処理(しょり) 属性(ぞくせい) (Form Handling Attributes)

キーワード説明(せつめい)(れい)
th:fieldフォームフィールドをバインド<input type="text" th:field="*{name}">
th:errorsフィールドバリデーションエラーを表示(ひょうじ)<span th:errors="*{name}">Name Error</span>
th:errorclassエラー発生時(はっせいじ) に CSS クラスを追加(ついか)<input th:field="*{name}" th:errorclass="is-invalid">
th:selected選択(せんたく) 状態(じょうたい)設定(せってい)<option th:selected="${user.role=='admin'}">Admin</option>
th:checkedチェック状態(じょうたい)設定(せってい)<input type="checkbox" th:checked="${user.active}">

反復(はんぷく)条件(じょうけん) 属性(ぞくせい) (Iteration and Conditional Attributes)

キーワード説明(せつめい)(れい)
th:eachコレクションを反復(はんぷく)<tr th:each="user, stat : ${users}">
th:if条件(じょうけん) 表示(ひょうじ)<div th:if="${user.age > 18}">
th:unless条件(じょうけん) 非表示(ひひょうじ)<div th:unless="${user.age > 18}">
th:switch/th:case複数(ふくすう) 条件(じょうけん) 切替(きりかえ)<div th:switch="${user.role}"><p th:case="'admin'">Admin</p></div>

コメントとブロック属性(ぞくせい) (Comment and Block Attributes)

キーワード説明(せつめい)(れい)
th:blockテンプレートフラグメント処理(しょり) コンテナ<th:block th:each="user : ${users}">Content</th:block>
<!--/* ... */-->パーサーレベルコメント (レンダリング()削除(さくじょ) )<!--/* Removed during rendering */-->
<!--/*/--><!--/*/-->条件(じょうけん) () きパーサーブロック<!--/*/--> Only shown during processing <!--/*/-->

インライン(しき) (Inline Expressions)

キーワード説明(せつめい)(れい)
[[...]]インラインテキスト<p>Welcome, [[${user.name}]]!</p>
[(...)]エスケープなしインラインテキスト<div>[(${htmlContent})]</div>
th:inline="text"テキスト(ない)(しき)使用(しよう)<script th:inline="text">var m = '[[${message}]]';</script>
th:inline="javascript"JS (ない)(しき)使用(しよう)<script th:inline="javascript">var u = [[${user}]];</script>
th:inline="css"CSS (ない)(しき)使用(しよう)<style th:inline="css">.u { color: [[${color}]]; }</style>

レイアウト属性(ぞくせい) (Layout Dialect Attributes)

キーワード説明(せつめい)(れい)
layout:decorateデコレートテンプレートを指定(してい)<html layout:decorate="~{layouts/default}">
layout:fragment置換(ちかん) 可能(かのう) なフラグメントを定義(ていぎ)<div layout:fragment="content">Page Content</div>
layout:insertフラグメントを挿入(そうにゅう)<div layout:insert="~{fragments/header :: header}"></div>
layout:replaceフラグメントを置換(ちかん)<div layout:replace="~{fragments/footer :: footer}"></div>

ユーティリティ関数(かんすう) (Utility Functions)

カテゴリプレフィックス(れい)
文字列(もじれつ)#strings${#strings.isEmpty(name)}
数値(すうち)#numbers${#numbers.formatDecimal(num, 1, 2)}
日付(ひづけ)#dates, #calendars, #temporals${#dates.format(date, 'yyyy-MM-dd')}
コレクション#lists, #sets, #maps${#lists.size(users)}
配列(はいれつ)#arrays${#arrays.length(userArray)}

実用(じつよう) ノート (Casual Notes)

  1. データ表示(ひょうじ)処理(しょり)

    • th:text: 基本的(きほんてき) なテキスト表示(ひょうじ)
    • th:utext: HTML タグを(ふく) むコンテンツの表示(ひょうじ)使用(しよう)
    • ${...}: 変数(へんすう) (しき) 、Model データにアクセス。
    • *{...}: 選択(せんたく) (しき)(なが) いオブジェクトパスを簡略化(かんりゃくか)
  2. 条件(じょうけん) とロジック制御(せいぎょ)

    • th:if / th:unless: 条件(じょうけん)(もと) づいて要素(ようそ)出力(しゅつりょく) するかを決定(けってい)
    • th:switch / th:case: ステートマシン(しき)表示(ひょうじ) ロジックに(てき) する。
    • th:with: 複雑(ふくざつ)(しき)簡略化(かんりゃくか) するローカル変数(へんすう)定義(ていぎ)
  3. コレクション処理(しょり)

    • th:each: データを反復(はんぷく) してテーブルやリストを生成(せいせい)
    • th:object: オブジェクトコンテキストを設定(せってい)
  4. フォーマットと特殊(とくしゅ) 処理(しょり)

    • #dates: 日付(ひづけ) をフォーマット。
    • #numbers: 金額(きんがく) 、パーセンテージなどの数値(すうち) フォーマットを処理(しょり)
    • #strings: 文字列(もじれつ) のトリム、置換(ちかん) など。

Flying Saucer (PDF 生成(せいせい) ) 関連(かんれん) 注意事項(ちゅういじこう)

  • スタイル処理(しょり) :
    • インラインスタイル (Inline Style) または内部(ないぶ) CSS を使用(しよう) してください。Flying Saucer は外部(がいぶ) スタイルシートを処理(しょり) できません。
    • th:styleth:class動的(どうてき) スタイル設定(せってい)使用(しよう) できます。
  • (かい) ページ制御(せいぎょ) :
    • CSS の page-break-before, page-break-after使用(しよう) して PDF の(かい) ページを制御(せいぎょ)
  • 画像(がぞう) 処理(しょり) :
    • th:src絶対(ぜったい) パスを使用(しよう) するか、パーサーが(ただ) しく識別(しきべつ) できるパスを確認(かくにん) してください。