LoginSignup
1
1

⚠️注意あり⚠️ ThymeleafテンプレートからJavaのStaticリソースにアクセスする方法

Posted at

Thymeleafテンプレートから、JavaのStaticリソースにアクセスしたいときってありませんか?(例えばEnumの要素をif判定で使いたいときとか)

そんなときThymeleafにはテンプレートからJavaのStaticリソースにアクセスできる便利な関数があります。

T()関数

JavaのMathクラスにあるstaticなリソース(ここではフィールドやメソッドを指しています)に対して、Thymeleafからアクセスしてみましょう。

sample.html
<!-- staticフィールドにアクセス -->
<input type="number" th:value="${T(java.lang.Math).PI}" />

<!-- staticメソッドにアクセス -->
<p th:text="${T(java.lang.Math).random()}">Random Number</p>
Math.java
/**
Mathクラスを一部抜粋
*/
public final class Math {
    // staticフィールド
    public static final double E = 2.7182818284590452354;
    public static final double PI = 3.14159265358979323846;

    // staticメソッド
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }
}

ここからわかるように、thタグにおいて、"${T(クラスパス).staticフィールド名}"もしくは"${T(クラスパス).staticメソッド名()}"としてあげることで、ThymeleafテンプレートからJavaのStaticリソースにアクセスすることができます!

他のthタグの例も見てみましょう。

sample.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<!-- th:text -->
<p th:text="${T(java.lang.Math).random()}">Random Number</p>

<!-- th:utext -->
<p th:utext="${T(java.lang.Math).random()}">Random Number</p>

<!-- th:if -->
<p th:if="${T(java.lang.Math).random() > 0.5}">Random number is greater than 0.5</p>

<!-- th:unless -->
<p th:unless="${T(java.lang.Math).random() <= 0.5}">Random number is less or equal to 0.5</p>

<!-- th:switch and th:case -->
<div th:switch="${T(java.lang.Math).PI}">
  <p th:case="${T(java.lang.Math).PI}">PI is equal to PI</p>
  <p th:case="${T(java.lang.Math).E}">PI is equal to E</p>
  <p th:case="*">PI is not equal to any known constants</p>
</div>

<!-- th:href -->
<a th:href="@{'/' + ${T(java.lang.Math).random()}}">Random Link</a>

<!-- th:each -->
<ul>
  <li th:each="item : ${T(java.util.Arrays).asList('Apple', 'Banana', 'Cherry')}">
    <span th:text="${item}">Item</span>
  </li>
</ul>

<!-- th:value -->
<input type="number" th:value="${T(java.lang.Math).PI}" />

<!-- th:with -->
<div th:with="randomNumber=${T(java.lang.Math).random()}">
  <p th:text="${randomNumber}">Random Number</p>
</div>

</body>
</html>

これを画面で表示してみると、こんな感じできちんとエラーなく表示されます。
image.png

注意せよ

Thymeleafのバージョンによって、T()関数が使えないthタグが存在するので注意が必要です!

私の調べた感じだと、thymeleaf-spring5ライブラリのバージョン3.0.12.RELEASEとバージョン3.0.13.RELEASEが境界地でした。(org.springframework.bootでいうと、2.6.12.6.2

thタグ 3.0.12以前 3.0.13以後
th:text T()関数使える T()関数使える
th:utext T()関数使える T()関数使えない
th:if T()関数使える T()関数使える
th:unless T()関数使える T()関数使える
th:switch T()関数使える T()関数使える
th:case T()関数使える T()関数使える
th:href T()関数使える T()関数使えない
th:each T()関数使える T()関数使える
th:value T()関数使える T()関数使える
th:with T()関数使える T()関数使える

もし、thymeleaf-spring5ライブラリのバージョン3.0.13.RELEASE以降において、th:href内でT()関数を使おうものなら、下記のようなエラーが発生するのでご注意ください。

java.lang.IllegalAccessError: 
class org.thymeleaf.spring5.dialect.SpringStandardDialect tried to access private field org.thymeleaf.standard.StandardDialect.conversionService

以上ですー。

1
1
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
1
1