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.

Revisions

  1. seanhinkley created this gist Feb 17, 2016.
    10 changes: 10 additions & 0 deletions BootApplication.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    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);
    }
    }
    63 changes: 63 additions & 0 deletions ThymeleafConfig.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    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;
    }


    }
    5 changes: 5 additions & 0 deletions application.properties
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    spring.thymeleaf.cache=false
    #idea flags this as invalid..
    spring.thymeleaf.mode=HTML

    ...snip...
    37 changes: 37 additions & 0 deletions pom.xml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <!-- 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>

    3 changes: 3 additions & 0 deletions spring.factories
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    #resources/META-INF/spring.factories
    #bootstrap in your thymeleaf 3 config
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=somewhere.outside.app.namespace.thymeleaf.ThymeleafConfig