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.isalnikov.config.web; import java.util.Locale; 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.ReloadableResourceBundleMessageSource; import org.springframework.web.servlet.LocaleResolver; 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.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.CookieLocaleResolver; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; /** * * @author Igor Salnikov */ @Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter { private static final int CACHE_PERIOD = 31556926;//one year @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); registry.addViewController("/user").setViewName("user"); registry.addViewController("/about").setViewName("about"); registry.addViewController("/admin").setViewName("admin"); registry.addViewController("/page1").setViewName("page1"); registry.addViewController("/page2").setViewName("page2"); registry.addViewController("/page3").setViewName("page3"); // registry.setOrder(Ordered.HIGHEST_PRECEDENCE); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/favicon.ico").addResourceLocations("/favicon.ico") .setCachePeriod(CACHE_PERIOD); //registry.addResourceHandler("/media/**").addResourceLocations("/media/").setCachePeriod(CACHE_PERIOD); registry.addResourceHandler("/media/**/*.{js,html,css}").addResourceLocations("/media/") .setCachePeriod(CACHE_PERIOD); registry.addResourceHandler("/resources/").addResourceLocations("/resources/**") .setCachePeriod(CACHE_PERIOD); } @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor(); interceptor.setParamName("language"); registry.addInterceptor(interceptor); } @Bean(name = "jspViewResolver") public InternalResourceViewResolver jspViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp"); resolver.setViewClass(JstlView.class); resolver.setOrder(1); return resolver; } @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); messageSource.setUseCodeAsDefaultMessage(true); messageSource.setDefaultEncoding("UTF-8"); messageSource.setBasenames("classpath:message"); messageSource.setCacheSeconds(5); messageSource.setFallbackToSystemLocale(false);// ??? return messageSource; } @Bean public LocaleResolver localeResolver() { CookieLocaleResolver localeResolver = new CookieLocaleResolver(); localeResolver.setDefaultLocale(new Locale("ru")); // change this Locale.ENGLISH) localeResolver.setCookieName("terminal_cookie"); localeResolver.setCookieMaxAge(60 * 30); return localeResolver; } }