Spring-boot: Support Thymeleaf 3

Created on 6 Nov 2015  路  63Comments  路  Source: spring-projects/spring-boot

Spring boot does not seem to play well with Thymeleaf 3 beta.

java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration due to internal class not found. 

This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake) 

....

 Caused by: java.lang.NoClassDefFoundError: org/thymeleaf/resourceresolver/IResourceResolver

Thymeleaf 3 is supported by Spring 4 but not Spring Boot (http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html)

Can we please add support for Thymeleaf 3 in the next version?

enhancement

Most helpful comment

I've just pushed support for Thymeleaf 3 to master. Thymeleaf 2 remains the default, but the auto-configuration should work if you override thymeleaf.version, thymeleaf-layout-dialect.version and the versions of any of the auto-configured extras that you're using. There's a new sample that shows this in action.

Please try out the snapshot that will be available once this build has completed from https://repo.spring.io/libs-snapshot and open an issue if you hit any problems with Boot's auto-configuration.

All 63 comments

We'll certainly consider it for 1.4. No chance for 1.3 I'm afraid (since Thymeleaf 3 is beta).

I am working on that in my own branch thymeleaf-3, I will update the branch with new versions :smiley:

:+1:

Awesome! Can't wait for it :)

IMO, it'd be interesting to explore supporting both versions of Thymeleaf in 1.4.

@eddumelendez @philwebb @wilkinsona please don't hesitate directly contacting us for anything you might need regarding Spring Boot integration. We'll be happy to help: http://www.thymeleaf.org/team.html

@wilkinsona I would like to be involved in how to handle the support for both versions. I already know that spring-framework use merge plugin in gradle which handle those things but in this case how it will be handled?

@eddumelendez Of course. I'm not sure how we'll handle it, or even if it needs to be handled at all. If it's easy for users to move from Thymeleaf 2 to 3 then it may not even be necessary.

@eddumelendez, are you able to integrate thymeleaf 3 successfully with spring boot? I am also waiting for thymeleaf3 integration with sping boot...

I put together a gist for spring boot w/ thymeleaf 3.0 (minus layout dialect). hope it helps!

https://gist.github.com/seanhinkley/6eab2130ceea857c160b

Thanks @seanhinkley !

Another user was confused a lot trying to integrate with thymeleaf 3 and wrote this: https://gist.github.com/seanhinkley/6eab2130ceea857c160b

Maybe we should try to integrate it for 1.4 still?

I think it's still too soon as Thymeleaf 3.0 is still in beta. They recently released beta 2. If a final release appears in time for 1.4 then we can look at it more seriously.

We have to wait the release of thymeleaf-layout-dialect too.

thoughts on adding @ConditionalOnMissingClass(name="org.thymeleaf.templatemode.TemplateMode'") to ThymeleafAutoConfiguration? This way boot won't try to autoconfig 2.1 if 3.0 was added to project.

It may also be useful if you plan on supporting both 2.1 and 3 initially.

@seanhinkley I like that, I've created #5371

I think that there are more classes involved, I have a branch ready to send the PR but just waiting the release of thymeleaf and thymeleaf-layout-dialect to run all the test again.

@eddumelendez were you replying to @seanhinkley's comment? If we could backoff when we detect Thymeleaf3 that would be nice I think. At least for the time being.

@snicoll I just submit an initial PR where we can see the changes. As I said before it's pending to update latest version for thymeleaf and thymeleaf-layout-dialect (supporting thymeleaf 3 too). I like the idea to support both versions too, I would like to discuss about how we can support both.

Thymeleaf 3.0.0 just released. Is it possible to merge https://gist.github.com/seanhinkley/6eab2130ceea857c160b to Spring 1.3.5 or 1.4.0?

http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html
First, we have good news: Your existing Thymeleaf templates are almost 100% compatible with Thymeleaf 3 so you will only have to do a few modifications in your configuration.

@linux-china It definitely won't go in 1.3.5; it's too big a change for a maintenance release. We can consider it for 1.4 but we'll need to be sure that it won't be too painful. For example, we'll need to check that all of the auto-configured dialects work with 3.0.

@wilkinsona I am working in update PR #5385 but we have to wait until the release of thymeleaf-layout-dialect (https://github.com/ultraq/thymeleaf-layout-dialect/issues/68)

You don't need thymeleaf-layout-dialect, I've removed it from my application and got a performance boost of 50ms/r (don't really know if it was a problem with the dialect, but that was the major change I made, try it yourself).
Thymeleaf 3 has great layout support with fragments.

By removing thymeleaf-layout-dialect you also get rid of the 5mb groovy dependency, which is also very good.

@jonhkr since spring-boot is supporting thymeleaf-layout-dialect we should keep it, unless spring-boot team decide to remove the dependency and the code related to this.

@eddumelendez I understand, there may also be legacy code that probably uses the dialect.

I would just take my comment above as a thing to be discussed in a major release. It's always good to remove unnecessary dependencies when it is possible.

@jonhkr got your point. The thymeleaf-layout-dialect dependency is a stopper right now and people can not use thymeleaf 3 automatically 馃槩, the last update was June 4.

馃槩

so What's the plan to support thymeleaf3 for spring-boot(version 1.4)?

@hotjavabilly We're currently blocked by the lack of a Thymeleaf 3.0-compatible version of Thymeleaf Layout Dialect. As things stand, it's more than likely that Boot 1.4 will stick with 2.1.

The author of thymeleaf-layout-dialect began to work again. Dont know if it will be released before 1.4 RC1

Please update!! Its a real blocker... 馃槩

@KarlKl If you read through the comments above, it's hopefully clear that we're blocked waiting for a Thymeleaf 3.0-compatbile version of the Layout Dialect. Your comment doesn't change that and just adds noise to the issue.

@wilkinsona I recently test thymeleaf-layout-dialect snapshots and it runs ok. But, there are one more dependency by spring-social too. I have submitted a PR to move the SpringSocialDialect to thymeleaf 3 https://github.com/spring-projects/spring-social/pull/208

Will thymeleaf 3.0 support be added to 1.4 if layout dialect is availiable?

No, I don't think so. We've released RC1 today so we've run out of time.

Could you maybe deprecate the thymeleaf-layout-dialect in 1.4, so Spring Boot 1.5 can ship without it (and with Thymeleaf 3 instead)? Or would that be too soon and we'll have to wait for 2.0?

For those interested in using Thymeleaf 3 with Spring Boot, I think it's worth noting that there's nothing stopping you from doing so. You'll just have to configure things manually as there's no auto-configuration for it.

If you are using Spring boot 1.3, then you'll need to disable ThymeleafAutoConfiguration and configure it manually. If you are using Spring Boot 1.4, then we automatically disable ThymeleafAutoConfiguration when Thymeleaf 3 is on the classpath so all that's left is the manual configuration.

I never disable ThymeleafAutoConfiguration with spring boot 1.3 and thymeleaf 3.0. I add another configuration.
https://github.com/zjnu-acm/judge/blob/master/src/main/java/cn/edu/zjnu/acm/judge/config/thymeleaf/ThymeleafConfiguration.java

With spring boot 1.3, I only need to declare 2 beans. But with spring boot 1.4, I need to declare more.

@zhanhb Interesting approach. It isn't doing exactly what (I assume) you think it is.

@AutoConfigureBefore and @AutoConfigureAfter will have no effect as, as far as I can tell, your ThymeleafConfiguration isn't an auto-configuration class. This means that your ThymeleafConfiguration will run before any auto-configuration.

I never disable ThymeleafAutoConfiguration with spring boot 1.3

You have effectively disabled some of it at least. For example, by creating a bean named defaultTemplateResolver. The condition on Boot's DefaultTemplateResolverConfiguration means that it will now be switched off.

But with spring boot 1.4, I need to declare more.

Can you provide some more details please? What extra beans do you need to declare and why do you believe you need to declare them?

Yeah, I do disable the effectivity of inner class in ThymeleafAutoConfiguration.

I don't care much about what AutoConfigureAfter and AutoConfigureBefore actually do, maybe I should remove them.

The more beans should be those delcared in the #5385 such as SpringTemplateEngine and other thymeleaf dialects.

https://github.com/zjnu-acm/judge/compare/6563cc23a83009595e4793c36eb3a7886726311a...0a1fac3b394a7763d1a32bed81a826ef76d8830c

The more beans should be those delcared in the #5385 such as SpringTemplateEngine and other thymelead dialects.

@zhanhb Sorry, I still don't understand. What's changed in Spring Boot 1.4 that means you need to declare more beans to use Thymeleaf 3 than you did when using Spring Boot 1.3?

Forgive my poor English

@ConditionalOnMissingClass("org.thymeleaf.templatemode.TemplateMode")

in class ThymeleafAutoConfiguration makes me to declare more beans.

@zhanhb No apology necessary! Thank you, I understand now

How to manually configure springboot 1.4 to use thymeleaf 3?
I configure dependency on pom.xml, but even simplest mapping to context path @RequestMapping(/) now show 404 error message : There was an unexpected error (type=Not Found, status=404).

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

Have you looked at the code that @zhanhb has linked to above?

Thymeleaf layout dialect 2.0.0 was released

I've just pushed support for Thymeleaf 3 to master. Thymeleaf 2 remains the default, but the auto-configuration should work if you override thymeleaf.version, thymeleaf-layout-dialect.version and the versions of any of the auto-configured extras that you're using. There's a new sample that shows this in action.

Please try out the snapshot that will be available once this build has completed from https://repo.spring.io/libs-snapshot and open an issue if you hit any problems with Boot's auto-configuration.

This looks awesome, thanks @wilkinsona

Hi, could you explain how to recreate the example with gradle? I tried to convert the project, but a gradle init on the example fails.

@thepete89 that should work

buildscript {
    ext {
        springBootVersion = '1.4.0.BUILD-SNAPSHOT'
    }
    repositories {
        mavenCentral()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

ext['thymeleaf.version'] = '3.0.0.RELEASE'
ext['thymeleaf-layout-dialect.version'] = '2.0.0'


dependencies {
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}


eclipse {
    classpath {
         containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
         containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
    }
}

@snicoll works - thank you!

Will this be incorporated in 1.4.0.RELEASE? I think we shouldn't include it in 1.4.0.RELEASE unless memory leak bug is fixed from layout dialect @ultraq, Refer https://github.com/ultraq/thymeleaf-layout-dialect/issues/122

@rajadileepkolli We're not changing the default Thymeleaf version in Spring Boot 1.4 but Thymeleaf 3 will be supported for those that wish to use it. See Andy's comment above.

Memory leak ultraq/thymeleaf-layout-dialect#122 seems to be fixed in Thymeleaf Layout Dialect 2.0.1

Can we see Thymeleaf 3 as default version in next minor release 1.5.x?

@rajadileepkolli Possibly, we've not started planning 1.5 yet.

with the Thymeleaf version 3, please consider replacing this dialect's with its pure Java version:
https://github.com/zhanhb/thymeleaf-layout-dialect
It's faster and smaller than groovy implementation.

@philwebb @snicoll Can we now have Thymeleaf 3 as default version in 1.5.x release of Spring boot?

@rajadileepkolli Thanks for the prod. I've opened #7450 to decided if we should do this in 1.5 or 2.0

Unfortunately, so far, the latest springboot still in the use of 2.1.5, not 3.x

@goahead77 this is closed for almost a year. Please read the doc. Spring Boot 2 uses Thymeleaf 3 by default. If you have more questions, please ask on stackoverflow.

Was this page helpful?
0 / 5 - 0 ratings