content: package com.yuu.ymall.web.api.common.config;
  
  
  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.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  import org.thymeleaf.TemplateEngine;
  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;
  
  /**
   * @author by Yuu
   * @classname ThymeleafConfig
   * @date 2019/6/28 16:17
   */
  @Configuration
  public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
  
      private ApplicationContext applicationContext;
  
      @Override
      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());
          return engine;
      }
  
      private ITemplateResolver templateResolver() {
          SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
          resolver.setApplicationContext(applicationContext);
          resolver.setPrefix("/WEB-INF/templates/");
          resolver.setSuffix(".html");
          resolver.setCharacterEncoding("UTF-8");
          resolver.setTemplateMode(TemplateMode.HTML);
          return resolver;
      }
  }
  