LoginSignup
3
3

More than 5 years have passed since last update.

Spring Bootでparentでspring-boot-starter-parentを指定せずにthymeleaf3を使う

Last updated at Posted at 2017-08-29

Springのドキュメントにも書いてありますが、Spring Bootでthymeleaf 3を使う時は、parentがspring-boot-starter-parentになっているとpropertiesにこんな感じで指定すると使えるようになります。

<properties>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>

でも、会社でparentに指定するpomが決まってるときはspring-boot-starter-parentをparent指定できません。
その場合にthymeleaf 3を使うようにする設定がややこしかったのでメモしておきます。

ここにも書いてますが、parentに指定しない場合は<dependencyManagement>にspring-boot-dependencyを書きます

<dependencyManagement>
     <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.5.6.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

これは何をしているかというとSpring Bootで必要となるdependenciesのバージョンを指定してくれます。
デフォで入るやつはこことか本家のpomに書いてあります。
このまま使うとthymeleaf 2.1.5がインストールされます。3を使いたいので<dependencyManagemet>のspring-boot-dependenciesの前に変更したいモジュールのバージョンとかを書いて上書きしてあげます。

<properties>
    <spring-boot.version>1.5.6.RELEASE</spring-boot.version>
    <thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
    <thymeleaf-spring4.version>3.0.7.RELEASE</thymeleaf-spring4.version>
    <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
  </properties>
<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>${thymeleaf.version}</version>
      </dependency>
      <dependency>
        <groupId>nz.net.ultraq.thymeleaf</groupId>
        <artifactId>thymeleaf-layout-dialect</artifactId>
        <version>${thymeleaf-layout-dialect.version}</version>
      </dependency>
      <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>${thymeleaf-spring4.version}</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

こんな感じです。
<dependencyManagement>に書くだけだと肝心のモジュールはインストールされないので、
<dependencies>にインストールするモジュールを書いてあげます。

<dependencies>
    <dependency>
   <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring4</artifactId>
    </dependency>
    <dependency>
      <groupId>nz.net.ultraq.thymeleaf</groupId>
      <artifactId>thymeleaf-layout-dialect</artifactId>
    </dependency>
  </dependencies>

こんな感じで!
もしspring-boot-starter-thymeleafがpomに書いてある場合は削除しておきます。

きっかけ

最初はthymeleafでLocalDateTimeを使いたかったので、thymeleaf-extras-java8timeを入れたんですが、
#temporals.format(from, 'yyyyMMdd')でfromがnullのときに、2系だとIllegalArgumentException: Cannot apply format on nullっていうのが出て使いにくいなと思ってたら、3.0.1で解決したっていうのを見つけました。これ
でも、thymeleaf-extras-java8timeの3系はthymeleaf 3でないとダメなので、とりあえず、propertiesでthymeleafのバージョンを3にすればいけるやろと思ったら、ClassNotFoundException IExpressionObjectDialectみたいなエラーがでて、調べてみるとpropertiesに指定するだけだとバージョンがthymeleaf 2のままになっていたという感じです。

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