LoginSignup
20
20

More than 5 years have passed since last update.

Spring Boot 1.4でThymeleaf 3.0系を使うための設定方法

Last updated at Posted at 2016-08-11

Spring BootでThymeleafを使おうと思うとデフォルトではThymeleaf 2.1系が使われますが、Spring Boot 1.4からは、Thymeleaf 3.0系もサポートされ自動コンフィギュレーションされます。

Thymeleaf 3.0系の適用方法

Spring Bootの公式サイトで紹介されている通り・・・・

  • Thymeleaf本体 + (Thymeleaf Spring)
  • Thymeleaf Layout Dialect
  • Spring Security Dialect (※利用する場合のみ)
  • Data Attribute Dialect (※利用する場合のみ)
  • Java 8 Time API Dialect (※利用する場合のみ)

のバージョン管理用のプロパティ値を変更するだけです。以下は、投稿時点(2016/8/11)の最新バージョンを利用する際のプロパティ値の設定例です。

pom.xml
<properties>
    <!-- ... -->
    <thymeleaf.version>3.0.1.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
    <thymeleaf-extras-springsecurity4.version>3.0.0.RELEASE</thymeleaf-extras-springsecurity4.version>
    <thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data-attribute.version>
    <thymeleaf-extras-java8time.version>3.0.0.RELEASE</thymeleaf-extras-java8time.version>
</properties>

Thymeleaf 3.0を適用してSpring Bootを起動してみると・・・

以下のようなWARNログがでます :sweat_smile:

コンソール
...
2016-08-11 11:21:11.924  WARN 56624 --- [  restartedMain] org.thymeleaf.templatemode.TemplateMode  : [THYMELEAF][restartedMain] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
...

これは、Spring BootのデフォルトがHTML5になっているためです・・・(Spring BootのデフォはあくまでThymeleaf 2.1系なので・・・)

テンプレートモードをHTMLにする

Thymeleaf 3.0系では"HTML5"というモードが非推奨ということなので、ログメッセージどおり推奨の"HTML"に変更しましょう。テンプレートモードを変更する場合は、以下のような設定を加えるだけです :v:

src/main/resources/application.properties
spring.thymeleaf.mode=HTML

Note: HTML5自体が非推奨というわけではない

あくまで"HTML5"というモードが非推奨になっただけで、HTML5が非推奨というわけではないので誤解しないでくださいね :sweat_smile:
http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html#full-html5-markup-support

まとめ

Thymeleaf 3.0系では、テンプレートを記載するマークアップ言語としてXHTMLじゃない普通のHTMLが利用できるようになったり、HTML系以外のモード(TEXT、JAVASCRIPT、CSS、RAW)が追加されていたりします。また、大幅なパフォーマンス改善が行われたらしいので、Thymeleaf 2系から乗り換えることを検討した方がよさげです。

テンプレートモードとしてTEXT指定すると、テキストメールを送る時のテンプレートエンジンとしてThymeleafが使えるのかな? FreeMarkerとかVelocityはもう不要!?(VelocityはSpring的に非推奨になったので使う気全くないけどw)

参考サイト

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