Created
February 17, 2016 16:49
-
-
Save seanhinkley/6eab2130ceea857c160b to your computer and use it in GitHub Desktop.
spring boot + thymeleaf 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spring.thymeleaf.cache=false | |
#idea flags this as invalid.. | |
spring.thymeleaf.mode=HTML | |
...snip... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#resources/META-INF/spring.factories | |
#bootstrap in your thymeleaf 3 config | |
org.springframework.boot.autoconfigure.EnableAutoConfiguration=somewhere.outside.app.namespace.thymeleaf.ThymeleafConfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
I believe it's easier then that options:
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-use-thymeleaf-3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@zhugw I'd suggest we still need this option:
as the option
HTML5
is depreceted inv3.0.0
and will be removed inv3.1
.If you found those default values in
org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
, which I checked as well inSpring Boot v1.5.4
, themode
field is currently set toHTML5
, and I assume it came from Thymeleaf 2. If you're not going to customize auto-configuration classThymeleafAutoConfiguration
to change the default value of it toHTML
, you cannot but use the application property above to change it manually. Technically the default valueHTML5
stands still for the legacy compatibility and is supposed work exactly same toHTML
, 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