@Configuration
@EnableWebMvc
@ComponentScan({ "ru.dev.avtonomki" })
public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver jspViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(jspTemplateResolver()));
resolver.setContentType("text/html");
resolver.setCharacterEncoding("UTF-8");
resolver.setOrder(1);
resolver.setViewNames(ArrayUtil.array("*.html"));
return resolver;
}
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.addDialect(new Java8TimeDialect());
engine.setTemplateResolver(templateResolver);
//engine.setTemplateEngineMessageSource(messageSource());
return engine;
}
private ITemplateResolver jspTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/");
//resolver.setSuffix(".jsp");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML5);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new NameFormatter());
}
}