Java tutorial
/* * Copyright (C) 2016 Federico Tello Gentile <federicotg@gmail.com> * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.fede.calculator.web; import java.util.Arrays; import java.util.Collections; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.http.MediaType; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.web.accept.ContentNegotiationManager; import org.springframework.web.accept.PathExtensionContentNegotiationStrategy; import org.springframework.web.servlet.View; 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.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; /** * * @author Federico Tello Gentile <federicotg@gmail.com> */ @EnableWebMvc @Configuration @ComponentScan(basePackages = { "org.fede.calculator.web.controller" }) public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/images/**").addResourceLocations("/images/") .addResourceLocations("/scripts/**").addResourceLocations("/scripts/") .addResourceLocations("/styles/**").addResourceLocations("/styles/"); } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setPrefix("/WEB-INF/jsp/"); bean.setSuffix(".jsp"); bean.setOrder(1); return bean; } @Bean public ContentNegotiatingViewResolver cnResolver() { ContentNegotiatingViewResolver bean = new ContentNegotiatingViewResolver(); bean.setOrder(0); bean.setContentNegotiationManager(new ContentNegotiationManager(new PathExtensionContentNegotiationStrategy( Collections.singletonMap("json", MediaType.APPLICATION_JSON)))); MappingJackson2JsonView jacksonView = new MappingJackson2JsonView(); jacksonView.setExtractValueFromSingleKeyModel(true); bean.setDefaultViews(Arrays.asList(new View[] { jacksonView })); return bean; } @Bean public AcceptHeaderLocaleResolver localeResolver() { return new AcceptHeaderLocaleResolver(); } @Bean public RequestMappingHandlerMapping handlerMapping() { return new RequestMappingHandlerMapping(); } @Override public Validator getValidator() { return new LocalValidatorFactoryBean(); } }