Skip to content

Instantly share code, notes, and snippets.

@seanhinkley
Created February 17, 2016 16:49
Show Gist options
  • Save seanhinkley/6eab2130ceea857c160b to your computer and use it in GitHub Desktop.
Save seanhinkley/6eab2130ceea857c160b to your computer and use it in GitHub Desktop.
spring boot + thymeleaf 3
spring.thymeleaf.cache=false
#idea flags this as invalid..
spring.thymeleaf.mode=HTML
...snip...
package com.boot;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
//make sure spring boot doesn't attempt 2.1 config
@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class} )
public class AppApplication {
public static void main(String[] args) {
SpringApplication.run(AppSecurityApplication.class, args);
}
}
<!-- use snapshot build -->
<repositories>
<repository>
<id>sonatype-nexus-snapshots</id>
<name>Sonatype Nexus Snapshots</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<!-- make sure you've removed this.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
#resources/META-INF/spring.factories
#bootstrap in your thymeleaf 3 config
org.springframework.boot.autoconfigure.EnableAutoConfiguration=somewhere.outside.app.namespace.thymeleaf.ThymeleafConfig
package somewhere.outside.app.namespace.thymeleaf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
@Configuration
@ConditionalOnClass({SpringTemplateEngine.class})
@EnableConfigurationProperties({ThymeleafProperties.class}) //no sense rolling our own.
@AutoConfigureAfter({WebMvcAutoConfiguration.class})
public class ThymeleafConfig implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Autowired
private ThymeleafProperties properties;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setOrder(2147483642);
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
//made this @Bean (vs private in Thymeleaf migration docs ), otherwise MessageSource wasn't autowired.
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix(this.properties.getPrefix());
resolver.setSuffix(this.properties.getSuffix());
resolver.setTemplateMode(this.properties.getMode());
resolver.setCacheable(this.properties.isCache());
return resolver;
}
}
@cexowengui
Copy link

thanks,bro. it is really helpful, finally I get the thymeleaf 3 run, the posts about this on google was drive me nuts. actully, I found there is no need for spring.factories, It works well.

@johnhall
Copy link

Worked great. All I needed to do was change templateEngine() to return SpringTemplateEngine, add the line engine.addDialect(new Java8TimeDialect()); and add 'org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.0.RELEASE' to the dependencies to get support for the LocalDate/LocalDateTime #temporals.format() capabilities. Thanks!

@kameshsampath
Copy link

excellent post ! works great! thanks for it

@wrpinheiro
Copy link

👍 Thank you for sharing! It surely saved me a lot of time dealing with these configurations :-)

@zhugw
Copy link

zhugw commented Jun 29, 2017

Thanks! And I used 3.0.6.RELEASE(without remove spring-boot-starter-thymeleaf)

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

I found no need to config below explicitly

spring.thymeleaf.cache=false
#idea flags this as invalid..
spring.thymeleaf.mode=HTML

because it has default value just like this

screen shot 2017-06-29 at 17 03 47

@zhugw
Copy link

zhugw commented Jun 29, 2017

Spring boot 1.5.4 replace default 2.1.5 thymeleaf with 3.0.6

pom.xml

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

just need use 3.0.6's dependency override default version, I have proved it works.

@djkeh
Copy link

djkeh commented Jul 25, 2017

@zhugw I'd suggest we still need this option:

spring.thymeleaf.mode=HTML

as the option HTML5 is depreceted in v3.0.0 and will be removed in v3.1.

If you found those default values in org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties, which I checked as well in Spring Boot v1.5.4, the mode field is currently set to HTML5, and I assume it came from Thymeleaf 2. If you're not going to customize auto-configuration class ThymeleafAutoConfiguration to change the default value of it to HTML, you cannot but use the application property above to change it manually. Technically the default value HTML5 stands still for the legacy compatibility and is supposed work exactly same to HTML, so keeping it won't cause any problem for now. But it's RECOMMENDED to change it.

Reference

http://www.thymeleaf.org/apidocs/thymeleaf/3.0.7.RELEASE/org/thymeleaf/templatemode/TemplateMode.html

@antoniolazaro
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment