Java tutorial
package com.miserablemind.butter.apps.butterApp; import com.miserablemind.butter.apps.butterApp.controller.ModelViewFillInterceptor; import com.miserablemind.butter.resolvers.user.ActiveUserMethodArgumentResolver; 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.method.support.HandlerMethodArgumentResolver; import org.springframework.web.servlet.ViewResolver; 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.view.InternalResourceViewResolver; import java.util.List; /* * Miserable Mind * http://www.butter.miserablemind.com * The MIT License (MIT) */ /** * MVC App Configuration for Butter App that configures a context to run in. * <p>Every App in the system should have a configuration file like this one.</p> * <p>The class implements {@link WebMvcConfigurerAdapter}, so the the default behavior of servlet could be overridden.</p> * * @author <a href="http://www.miserablemind.com" target="_blank">miserablemind</a> */ @Configuration @EnableWebMvc @ComponentScan({ "com.miserablemind.butter.apps.butterApp.controller" }) public class ButterAppMVCContext extends WebMvcConfigurerAdapter { /** * {@inheritDoc} * Adds all argument resolvers to MVC app. In this case it adds a resolver for @ActiveUser. * * @param argumentResolvers a list resolvers if any exist, otherwise empty */ @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new ActiveUserMethodArgumentResolver()); } /** * {@inheritDoc} * Adds Spring MVC lifecycle interceptors for pre- and post-processing of controller method invocations. * * @param registry a list interceptors if any exist, otherwise empty * @see #modelViewFillInterceptor */ @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(this.modelViewFillInterceptor()); } /** * Interceptor bean that populates {@code model} in its postHandle with configuration object before handing it over to view. * <p>Note: postHandle does not get invoked if controller threw an error.</p> * <p>Interceptor needs to be registered with the container by {@link #addInterceptors}</p> * * @return interceptor bean */ @Bean public ModelViewFillInterceptor modelViewFillInterceptor() { return new ModelViewFillInterceptor(); } /** * Registers resource handlers for static css, js and img * * @param registry to register handler at */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/css/**").addResourceLocations("/resources/css/").setCachePeriod(31556926); registry.addResourceHandler("/js/**").addResourceLocations("/resources/js/").setCachePeriod(31556926); registry.addResourceHandler("/img/**").addResourceLocations("/resources/img/").setCachePeriod(31556926); } /** * View resolver bean configuration. It defines prefix and suffix of view files, so that logical view names could be resolved. * * @return a resolver the app will use */ @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/pageTemplates/butterApp/"); resolver.setSuffix(".jsp"); return resolver; } /** * Message Source bean that is used to get language from messages.properties. * <p>The bean can be used for errors or other non-template generated copy.</p> */ @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); messageSource.setBasename("butterApp/messages"); messageSource.setCacheSeconds(600); return messageSource; } }