title: Thymeleaf Template Engine Bean ignored
body: <p>I have a Spring Application, but my Config for Thymeleaf is completely ignored. What am I doing wrong? I need Thymeleaf to process external Javascript files, but without a working Config I cant add a Javascript Templateresolver.</p>
  
  <p>I know that its not working because Thymeleaf tells me that HTML5 mode is deprecated, but as you can see I set the mode to HTML.</p>
  
  <pre><code>package de.xy;
  
  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.domain.EntityScan;
  import org.springframework.boot.builder.SpringApplicationBuilder;
  import org.springframework.boot.web.support.SpringBootServletInitializer;
  import org.springframework.scheduling.annotation.EnableScheduling;
  
  @SpringBootApplication
  @EnableScheduling
  @EntityScan("de.xy.model")
  public class Application extends SpringBootServletInitializer {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Override
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(Application.class);
      }
  }
  </code></pre>
  
  <p>Config</p>
  
  <pre><code>package de.xy.config;
  
  import org.springframework.context.ApplicationContext;
  import org.springframework.context.ApplicationContextAware;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.ViewResolver;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  import org.thymeleaf.TemplateEngine;
  import org.thymeleaf.extras.springsecurity4.dialect.SpringSecurityDialect;
  import org.thymeleaf.spring4.SpringTemplateEngine;
  import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
  import org.thymeleaf.spring4.view.ThymeleafViewResolver;
  import org.thymeleaf.templatemode.TemplateMode;
  import org.thymeleaf.templateresolver.ITemplateResolver;
  
  @Configuration
  @EnableWebMvc
  @ComponentScan("de.xy")
  public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
  
      private ApplicationContext applicationContext;
  
      public void setApplicationContext(ApplicationContext applicationContext) {
          this.applicationContext = applicationContext;
      }
  
      @Bean
      public ViewResolver viewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine());
          resolver.setCharacterEncoding("UTF-8");
          return resolver;
      }
  
      @Bean
      public TemplateEngine templateEngine() {
          SpringTemplateEngine engine = new SpringTemplateEngine();
          engine.setEnableSpringELCompiler(true);
          engine.setTemplateResolver(templateResolver());
          engine.addDialect(new SpringSecurityDialect());
          return engine;
      }
  
      private ITemplateResolver templateResolver() {
          SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
          resolver.setApplicationContext(applicationContext);
          resolver.setPrefix("/WEB-INF/templates/");
          resolver.setTemplateMode(TemplateMode.HTML);
          return resolver;
      }
  }
  </code></pre>
  
  <p>EDIT:<br>
  Working Config:</p>
  
  <pre><code>@Configuration
  public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
  
      private static final String UTF8    = "UTF-8";
  
      private ApplicationContext  applicationContext;
  
      @Override
      public void setApplicationContext(ApplicationContext applicationContext) {
          this.applicationContext = applicationContext;
      }
  
      @Bean
      public ViewResolver htmlViewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
          resolver.setContentType("text/html");
          resolver.setCharacterEncoding(UTF8);
          resolver.setViewNames(new String[] { "*.html" });
          return resolver;
      }
  
      @Bean
      public ViewResolver cssViewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine(cssTemplateResolver()));
          resolver.setContentType("text/css");
          resolver.setCharacterEncoding(UTF8);
          resolver.setViewNames(new String[] { "*.css" });
          return resolver;
      }
  
      @Bean
      public ViewResolver javascriptViewResolver() {
          ThymeleafViewResolver resolver = new ThymeleafViewResolver();
          resolver.setTemplateEngine(templateEngine(javascriptTemplateResolver()));
          resolver.setContentType("application/javascript");
          resolver.setCharacterEncoding(UTF8);
          resolver.setViewNames(new String[] { "*.js" });
          return resolver;
      }
  
      private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
          SpringTemplateEngine engine = new SpringTemplateEngine();
          engine.setTemplateResolver(templateResolver);
          return engine;
      }
  
      private ITemplateResolver htmlTemplateResolver() {
          SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
          resolver.setApplicationContext(applicationContext);
          resolver.setPrefix("/WEB-INF/templates/");
          resolver.setTemplateMode(TemplateMode.HTML);
          return resolver;
      }
  
      private ITemplateResolver cssTemplateResolver() {
          SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
          resolver.setApplicationContext(applicationContext);
          resolver.setPrefix("/WEB-INF/css/");
          resolver.setTemplateMode(TemplateMode.CSS);
          return resolver;
      }
  
      private ITemplateResolver javascriptTemplateResolver() {
          SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
          resolver.setApplicationContext(applicationContext);
          resolver.setPrefix("/WEB-INF/js/");
          resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
          return resolver;
      }
  }
  </code></pre>
  
  <p>JavascriptController:</p>
  
  <pre><code>@Controller
  @RequestMapping("js")
  public class JavascriptController {
  
      @RequestMapping("somejavascriptfile.js")
      public String getJS() {
          return "somejavascriptfile.js";
      }}
  </code></pre>
  
  <p>It is more than I need, but it works (Controller does nothing)</p>