Created
May 8, 2014 22:14
-
-
Save thysmichels/5ef87db969e88f71f7ad to your computer and use it in GitHub Desktop.
WebMvcConfigurationSupport
This file contains 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.example.config; | |
import static org.springframework.context.annotation.ComponentScan.Filter; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.ComponentScan; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.data.repository.support.DomainClassConverter; | |
import org.springframework.format.support.FormattingConversionService; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.multipart.MultipartResolver; | |
import org.springframework.web.multipart.support.StandardServletMultipartResolver; | |
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.WebMvcConfigurationSupport; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import org.springframework.web.servlet.view.InternalResourceViewResolver; | |
import org.springframework.web.servlet.view.JstlView; | |
@EnableWebMvc | |
@Configuration | |
@ComponentScan(basePackages={"com.example.service"}, includeFilters = @Filter(Controller.class), useDefaultFilters = false) | |
class WebMvcConfig extends WebMvcConfigurationSupport { | |
private static final String RESOURCES_HANDLER = "/resources/"; | |
private static final String RESOURCES_LOCATION = RESOURCES_HANDLER + "**"; | |
@Bean | |
public DomainClassConverter<?> domainClassConverter() { | |
return new DomainClassConverter<FormattingConversionService>(mvcConversionService()); | |
} | |
@Override | |
public RequestMappingHandlerMapping requestMappingHandlerMapping() { | |
RequestMappingHandlerMapping requestMappingHandlerMapping = super.requestMappingHandlerMapping(); | |
requestMappingHandlerMapping.setUseSuffixPatternMatch(false); | |
requestMappingHandlerMapping.setUseTrailingSlashMatch(false); | |
return requestMappingHandlerMapping; | |
} | |
@Override | |
public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
registry.addResourceHandler(RESOURCES_LOCATION).addResourceLocations(RESOURCES_HANDLER); | |
} | |
@Override | |
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { | |
configurer.enable(); | |
} | |
@Bean | |
public MultipartResolver multipartResolver() { | |
return new StandardServletMultipartResolver(); | |
} | |
@Bean | |
public ViewResolver internalResourceViewResolver() { | |
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver(); | |
internalResourceViewResolver.setViewClass(JstlView.class); | |
internalResourceViewResolver.setPrefix("/WEB-INF/jsp/"); | |
internalResourceViewResolver.setSuffix(".jsp"); | |
internalResourceViewResolver.setRedirectHttp10Compatible(false); | |
return internalResourceViewResolver; | |
} | |
/** | |
* Handles favicon.ico requests assuring no <code>404 Not Found</code> error is returned. | |
*/ | |
@Controller | |
static class FaviconController { | |
@RequestMapping("favicon.ico") | |
String favicon() { | |
return "forward:/resources/images/favicon.ico"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment