|
| 1 | +package com.baeldung.thymeleaf.config; |
| 2 | + |
| 3 | +import com.baeldung.thymeleaf.formatter.NameFormatter; |
| 4 | +import com.baeldung.thymeleaf.utils.ArrayUtil; |
| 5 | +import nz.net.ultraq.thymeleaf.LayoutDialect; |
| 6 | +import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy; |
| 7 | +import org.springframework.context.ApplicationContext; |
| 8 | +import org.springframework.context.ApplicationContextAware; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.context.annotation.ComponentScan; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | +import org.springframework.context.annotation.Description; |
| 13 | +import org.springframework.context.support.ResourceBundleMessageSource; |
| 14 | +import org.springframework.format.FormatterRegistry; |
| 15 | +import org.springframework.web.servlet.LocaleResolver; |
| 16 | +import org.springframework.web.servlet.ViewResolver; |
| 17 | +import org.springframework.web.servlet.config.annotation.EnableWebMvc; |
| 18 | +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
| 19 | +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; |
| 20 | +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 21 | +import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; |
| 22 | +import org.springframework.web.servlet.i18n.SessionLocaleResolver; |
| 23 | +import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect; |
| 24 | +import org.thymeleaf.spring5.ISpringTemplateEngine; |
| 25 | +import org.thymeleaf.spring5.SpringTemplateEngine; |
| 26 | +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; |
| 27 | +import org.thymeleaf.spring5.view.ThymeleafViewResolver; |
| 28 | +import org.thymeleaf.templatemode.TemplateMode; |
| 29 | +import org.thymeleaf.templateresolver.ITemplateResolver; |
| 30 | + |
| 31 | +import java.util.Locale; |
| 32 | + |
| 33 | +@Configuration |
| 34 | +@EnableWebMvc |
| 35 | +@ComponentScan({ "com.baeldung.thymeleaf" }) |
| 36 | +/* |
| 37 | + Java configuration file that is used for Spring MVC and Thymeleaf |
| 38 | + configurations |
| 39 | + */ |
| 40 | +public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware { |
| 41 | + |
| 42 | + private ApplicationContext applicationContext; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 46 | + this.applicationContext = applicationContext; |
| 47 | + } |
| 48 | + |
| 49 | + @Bean |
| 50 | + public ViewResolver htmlViewResolver() { |
| 51 | + ThymeleafViewResolver resolver = new ThymeleafViewResolver(); |
| 52 | + resolver.setTemplateEngine(templateEngine(htmlTemplateResolver())); |
| 53 | + resolver.setContentType("text/html"); |
| 54 | + resolver.setCharacterEncoding("UTF-8"); |
| 55 | + resolver.setViewNames(ArrayUtil.array("*.html")); |
| 56 | + return resolver; |
| 57 | + } |
| 58 | + |
| 59 | + private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) { |
| 60 | + SpringTemplateEngine engine = new SpringTemplateEngine(); |
| 61 | + engine.addDialect(new LayoutDialect(new GroupingStrategy())); |
| 62 | + engine.addDialect(new Java8TimeDialect()); |
| 63 | + engine.setTemplateResolver(templateResolver); |
| 64 | + engine.setTemplateEngineMessageSource(messageSource()); |
| 65 | + return engine; |
| 66 | + } |
| 67 | + |
| 68 | + private ITemplateResolver htmlTemplateResolver() { |
| 69 | + SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); |
| 70 | + resolver.setApplicationContext(applicationContext); |
| 71 | + resolver.setPrefix("/WEB-INF/views/"); |
| 72 | + resolver.setCacheable(false); |
| 73 | + resolver.setTemplateMode(TemplateMode.HTML); |
| 74 | + return resolver; |
| 75 | + } |
| 76 | + |
| 77 | + @Bean |
| 78 | + @Description("Spring Message Resolver") |
| 79 | + public ResourceBundleMessageSource messageSource() { |
| 80 | + ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); |
| 81 | + messageSource.setBasename("messages"); |
| 82 | + return messageSource; |
| 83 | + } |
| 84 | + |
| 85 | + @Bean |
| 86 | + public LocaleResolver localeResolver() { |
| 87 | + SessionLocaleResolver localeResolver = new SessionLocaleResolver(); |
| 88 | + localeResolver.setDefaultLocale(new Locale("en")); |
| 89 | + return localeResolver; |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + public LocaleChangeInterceptor localeChangeInterceptor() { |
| 94 | + LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor(); |
| 95 | + localeChangeInterceptor.setParamName("lang"); |
| 96 | + return localeChangeInterceptor; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void addInterceptors(InterceptorRegistry registry) { |
| 101 | + registry.addInterceptor(localeChangeInterceptor()); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void addResourceHandlers(ResourceHandlerRegistry registry) { |
| 106 | + registry.addResourceHandler("/resources/**", "/css/**") |
| 107 | + .addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/"); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + @Description("Custom Conversion Service") |
| 112 | + public void addFormatters(FormatterRegistry registry) { |
| 113 | + registry.addFormatter(new NameFormatter()); |
| 114 | + } |
| 115 | +} |
0 commit comments