Thymeleaf 「th:if」属性 「th:unless」属性 「th:switch」属性

<span th:if="${#fields.hasErrors('name')}" th:errors="*{name}" style="color:red"></span>
<div th:if="${session.bean == null}"> セッション中のスコープ変数 bean の値が null の場合 </div>

また、「th:if 」属性の値が null でない場合に true と判断する式のルールは以下の通り。
※ 値が null の場合は false と判断。
 boolean 型の true
 0 以外の数値
 0 以外の文字
 「 false 」、「 off 」、「 no 」以外の文字列
 真偽値、数値、文字、文字列以外の場合


また、「th:if 」とは逆に、条件を満たさない場合に特定の情報を出力する属性として「 th:unless 」属性
がある。

<span th:unless="${#fields.hasErrors('name')}" th:text="正常値です "></span>

Thymeleafでif-else文を作りたいのならばth:ifとth:unlessを組み合わせるのもいいかも


switch 文は「 th:switch 」属性と「 th:case 」属性を使用。「 th:switch 」属性には、評価の対象を記述。 そして、「 th:case 」属性には値を記述。
いずれかの「th:case 」属性の値が「 th:switch 」属性の値と一致する場合、対象の「 th:case 」属性の判
定 結果は true と評価され、対象のタグの処理結果が出力される。そして、それ以降の「 th:case 」属性
は 全て false と評価される。
また、「いずれの値にも一致しない場合」という条件で判定したい場合は「 th:case="*" 」と記述。

<div th:switch ="${user.Id}">
 <p th:case="1" 開発部 </p>
 <p th:case="2" 営業部 </p>
 <p th:case="*" 部署に所属していません </p>
</div>