Created
January 26, 2014 05:52
-
-
Save bava/8629043 to your computer and use it in GitHub Desktop.
Spring's Java based Configuration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.inflinx.springmvc.config; | |
| 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.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.ResourceHandlerRegistry; | |
| import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; | |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; | |
| import org.springframework.web.servlet.view.InternalResourceViewResolver; | |
| import org.springframework.web.servlet.view.JstlView; | |
| @Configuration | |
| @EnableWebMvc | |
| @ComponentScan(basePackages = {"com.inflinx.spring4.web"} ) | |
| public class WebConfig extends WebMvcConfigurerAdapter { | |
| @Override | |
| public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { | |
| configurer.enable(); | |
| } | |
| @Override | |
| public void addViewControllers(ViewControllerRegistry viewControllerRegistry) { | |
| viewControllerRegistry.addViewController("/").setViewName("index"); | |
| } | |
| @Bean | |
| public ViewResolver viewResolver() { | |
| InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); | |
| viewResolver.setViewClass(JstlView.class); | |
| viewResolver.setPrefix("/WEB-INF/views/"); | |
| viewResolver.setSuffix(".jsp"); | |
| return viewResolver; | |
| } | |
| @Bean | |
| public MessageSource messageSource() { | |
| ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); | |
| messageSource.setBasenames("messages/messages"); | |
| return messageSource; | |
| } | |
| @Override | |
| public void addResourceHandlers(ResourceHandlerRegistry resourceHandlerRegistry) { | |
| resourceHandlerRegistry.addResourceHandler("/scripts/**").addResourceLocations("/js/").setCachePeriod(31556926); | |
| resourceHandlerRegistry.addResourceHandler("/styles/**").addResourceLocations("/styles/").setCachePeriod(31556926); | |
| resourceHandlerRegistry.addResourceHandler("/images/**").addResourceLocations("/images/").setCachePeriod(31556926); | |
| // Include static content from web jars | |
| resourceHandlerRegistry.addResourceHandler("/static/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(31556926); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment