Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.vgorcinschi.concordiafootballmanager.web; import java.nio.charset.StandardCharsets; import java.util.Locale; import javax.inject.Inject; import org.hibernate.validator.HibernateValidator; import org.springframework.context.MessageSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ResourceBundleMessageSource; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.SpringValidatorAdapter; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; /** * * @author vgorcinschi */ @Configuration @EnableWebMvc @ComponentScan(basePackages = { "com.vgorcinschi.concordiafootballmanager.web", "com.vgorcinschi.concordiafootballmanager.rest" }/*, useDefaultFilters = false, includeFilters = { @Filter(type = FilterType.ANNOTATION, value = org.springframework.stereotype.Controller.class), @Filter(ControllerAdvice.class)}*/) public class WebConfig extends WebMvcConfigurerAdapter { @Inject SpringValidatorAdapter springValidator; @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); //resolver.setExposeContextBeansAsAttributes(true); resolver.setViewClass(JstlView.class); resolver.setOrder(0); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // TODO Auto-generated method stub super.addResourceHandlers(registry); } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setCacheSeconds(1); messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name()); messageSource.setBasename("i18n/validation"); return messageSource; } @Bean public LocalValidatorFactoryBean localValidatorFactoryBean() throws ClassNotFoundException { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setProviderClass(HibernateValidator.class); validator.setValidationMessageSource(this.messageSource()); return validator; } @Override public void addInterceptors(InterceptorRegistry registry) { super.addInterceptors(registry); LocaleChangeInterceptor lCi = new LocaleChangeInterceptor(); lCi.setParamName("myLocale"); registry.addInterceptor(lCi); } @Bean public LocaleResolver localeResolver() { SessionLocaleResolver resolver = new SessionLocaleResolver(); resolver.setDefaultLocale(new Locale("en")); return resolver; } @Override public Validator getValidator() { return this.springValidator; } }