Search code examples
spring-mvcthymeleaf

Thymeleaf migration from 2 to 3 getting exception with use of thymeleaf-extras-conditionalcomments


I would like to migrate from Thymeleaf 2.1.3 to 3.0.8. I updated the pom.xml file like you can see below with the newest version of the resoucres. If I try to deploy the war file to Jboss I'm getting an BeanCreationException. The detailed Stacktrace you can find below.

I guess the exception is raised due to missing attributes in the superclass constructor of ConditionalCommentsDialect. ConditionalCommentsDialect is extending org.thymeleaf.dialect.AbstractDialect and the constructor is changed in the Thymeleaf 3 version. It seems that the ConditionalCommentsDialect is not compatible with Thymeleaf 3. So is my assumption right? Any ideas how to solve this? Thanks in advance.

pom.xml

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.8.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-spring4</artifactId>
  <version>3.0.8.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf-testing</artifactId>
  <version>3.0.3.RELEASE</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>org.attoparser</groupId>
  <artifactId>attoparser</artifactId>
  <version>2.0.4.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.thymeleaf.extras</groupId>
  <artifactId>thymeleaf-extras-conditionalcomments</artifactId>
  <version>2.1.2.RELEASE</version>
</dependency>

<dependency>
  <groupId>nz.net.ultraq.thymeleaf</groupId>
  <artifactId>thymeleaf-layout-dialect</artifactId>
  <version>2.2.2</version>
</dependency>

spring configuration file

<util:set id="extraDialects">
  <bean class="nz.net.ultraq.thymeleaf.LayoutDialect"/>
  <bean class="org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect"/>
</util:set>

<bean id="templateResolver"
      class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"
      p:prefix="classpath:/views/" p:suffix=".html"
      p:templateMode="HTML5" p:characterEncoding="UTF-8"/>

<bean id="templateEngine"
      class="org.thymeleaf.spring4.SpringTemplateEngine"
      p:templateResolver-ref="templateResolver"
      p:extraDialects-ref="extraDialects"/>

<bean id="viewResolver"
      class="org.thymeleaf.spring4.view.ThymeleafViewResolver"
      p:templateEngine-ref="templateEngine"
      p:characterEncoding="UTF-8"/>

Exception

Error creating bean with name 'extraDialects': Cannot create inner bean org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect#503d1586' of type [org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect] while setting bean property 'sourceSet' with key [1];
    nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect#503d1586' defined in class path resource [spring-webmvc.xml]: Instantiation of bean failed;
    nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect]: Constructor threw exception;
    nested exception is java.lang.NoSuchMethodError: org.thymeleaf.dialect.AbstractDialect: method <init>()V not found

Solution

  • Conditional comments are now provided as part of the Thymeleaf core (since version 3). You can simply remove that dependency.