在Spring Boot中,如果不指定spring-boot-starter-parent作为父项目,可以使用Thymeleaf3

在Spring的文档中也有提到,当在Spring Boot中使用thymeleaf 3时,如果父项目为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>

然而,在公司指定了pom作为parent时,无法使用spring-boot-starter-parent作为parent指定。
在这种情况下,设置thymeleaf 3的配置会有些复杂,所以我做了一些笔记。

如果没有指定parent,那么在中写入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版本,需要在的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>

就像这样。
只写在中并不能安装必要的模块,
所以要在中写上要安装的模块。

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

如果pom文件里写有spring-boot-starter-thymeleaf,我会删除它。

引发

最初我想在thymeleaf中使用LocalDateTime,所以我安装了thymeleaf-extras-java8time。但是当使用#temporals.format(from, ‘yyyyMMdd’)时,如果from为空,2系列会出现IllegalArgumentException: Cannot apply format on null的错误,感觉不太方便。后来在3.0.1版本中发现这个问题得到解决。但是,thymeleaf-extras-java8time的3系列必须与thymeleaf 3配套使用。所以,我尝试在properties中将thymeleaf的版本设置为3,结果遇到了ClassNotFoundException IExpressionObjectDialect这样的错误。经过调查我发现,仅仅在properties中指定版本并没有起作用,thymeleaf仍然保持在2的版本。

bannerAds