Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top