26

I'm using Spring Boot and I want to add the IE conditional comments Thymeleaf dialect.

I've included it in my maven pom.xml, but it's not working. How do I tell Thymeleaf to use it?

3 Answers 3

35

NOTE: Before trying this, note that later versions of Spring Boot include some of the common dialects out of the box. See @Robert Hunt's answer. Otherwise:

There is an example here of adding Dialect beans, which Spring Boot will automagically detect and use (see the LayoutDialect code and the dialects member of the ThymeleafDefaultConfiguration class). In your case, add the following in of one of your @Configuration classes:

@Bean
public ConditionalCommentsDialect conditionalCommentDialect() {
    return new ConditionalCommentsDialect();
}

Spring Boot, in the ThymeleafAutoConfiguration class, will automatically add any Beans that implement the IDialect interface.

2
  • 23
    Why did you answer your own question in the second person?
    – xdhmoore
    Jun 20, 2014 at 14:22
  • your comment took me longer than it should... anyway, thanks for sharing! Mar 31, 2015 at 21:09
14

With the release of Spring Boot 1.2.1 a few additional dialects have been added to the ThymeleafAutoConfiguration class which will be auto detected if they are on the classpath including:

Simply having the JAR on the classpath is enough for Spring Boot to register them.

Note: If you are using spring-boot-starter-thymeleaf then you'll find that the LayoutDialect is already included by default.

4
  • I am a bit confused, can you please help. spring-boot-starter-parent's latest revision is 1.5.2. Using this configuration, the version of thymeleaf resolves to 2.something. Whereas according to the ultraq.github.io/thymeleaf-layout-dialect/Installation.html, the layout dialect requires thymeleaf 3.0 Mar 17, 2017 at 15:23
  • 1
    @djaqeel version 2.0.0 of the layout dialect and beyond support thymeleaf 3. The website is a bit ambigious because it says "Minimum of Java 7 and Thymeleaf 3.0 required" but in the "Migrating to 2.0" guide at the bottom of the page it says "Thymeleaf 3 is largely backwards compatible with Thymeleaf 2 templates, and so the layout dialect has gone out of its way to be backwards compatible too." so it might just be worth trying it. Spring Boot 1.5.2 has a dependency managed version for "thymeleaf-layout-dialect" set to version 1.4.0 so it will work fine with that version. Mar 17, 2017 at 15:30
  • Thanks. Is there any link to layout dialect 1.4.0 tutorial. Because ultraq.github.io/thymeleaf-layout-dialect/Examples.html#layouts does not seem to work for me. layout file is not being applied. Mar 17, 2017 at 15:38
  • 4
    @djaqeel you might just be better off using thymeleaf 3 with Spring Boot 1.5.2 as it does support it. If you're using Maven and inheriting from spring-boot-starter-parent (or including the BOM) just define a property: <thymeleaf.version>3.0.3.RELEASE</thymeleaf.version> you may also need to set <thymeleaf-layout-dialect.version>2.2.1</thymeleaf-layout-dialect.version> to bump up the layout dialect version. Mar 17, 2017 at 15:54
5

I actually think there is defect in ThymeleafAutoConfiguration. I see the code where it is supposed to pick up and add SpringSecurityDialect to the Config if it's on the classpath, but in my debug, this is simply not happening (only LayoutDialect gets directed and added to the config). I have the SpringSecurityDialect class/jar on my classpath, but the bean below is never added to the config by SpringBoot AutoConfig (ThymeleafAutoConfig.java,line 97)

  @Configuration
@ConditionalOnClass({SpringSecurityDialect.class})
protected static class ThymeleafSecurityDialectConfiguration {
    protected ThymeleafSecurityDialectConfiguration() {
    }

    @Bean
    @ConditionalOnMissingBean
    public SpringSecurityDialect securityDialect() {
        return new SpringSecurityDialect();
    }
}

In the end, I had to actually add a bean to my custom Java config to get the SpringSecurityDialog recognized:

@Bean
public SpringSecurityDialect securityDialect() {
    return new SpringSecurityDialect();
}

This worked first time. May you have some tests to verify this is a known issue or not? I include my pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <version>2.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
4
  • It should work without having to specify the bean manually. I've not tested it with Spring Boot 1.2.5 but the support was added in 1.2.1 so it should work. I did checkout this sample: github.com/spring-projects/spring-boot/tree/master/… and added the "thymeleaf-extras-springsecurity4" dependency and it got registered automatically. You might want to run the application with the --debug flag or add spring-boot-actuator and read through the auto configuration report. Oct 30, 2015 at 15:49
  • Thank you, I have same problem with 1.3.3.RELEASE and defintion fix it. Also it seems to be fixed in 1.4. Mar 23, 2016 at 9:39
  • 1
    I am still getting Exception in thread "main" java.lang.NoClassDefFoundError: org/thymeleaf/dialect/IExpressionEnhancingDialect even after I added all your suggestions
    – yglodt
    Dec 29, 2016 at 22:24
  • @yglodt using version 3.0.1.RELEASE of the thymeleaf extras springsecurity fixes that problem. Jan 14, 2017 at 21:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.