我想添加资源处理程序 . 在论坛中他们使用 WebMvcConfigurationSupport
:http://forum.springsource.org/showthread.php?116068-How-to-configure-lt-mvc-resources-gt-mapping-to-take-precedence-over-RequestMapping&p=384066#post384066
和文档说 WebMvcConfigurerAdapter
:http://static.springsource.org/spring/docs/3.2.x/javadoc-api/org/springframework/web/servlet/config/annotation/EnableWebMvc.html
有什么区别,使用哪一个?两者都有我需要的 addResourceHandlers
方法 .
这是我目前的课程:
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
public @Override void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources");
}
public @Bean TilesViewResolver tilesViewResolver() {
return new TilesViewResolver();
}
public @Bean TilesConfigurer tilesConfigurer() {
TilesConfigurer ret = new TilesConfigurer();
ret.setDefinitions(new String[] { "classpath:tiles.xml" });
return ret;
}
}
3 回答
答案在上面引用的文档中:
简而言之,如果@EnableWebMvc适合您,则无需进一步查看 .
它更好地扩展WebMvcConfigurationSupport . 它提供了更多的自定义选项,也可以正常使用
因为你可以使用添加这些转换器
WebMvcConfigurerAdapter不具备此功能 .
点击[这里] How to configure MappingJacksonHttpMessageConverter while using spring annotation-based configuration?
如果扩展WebMvcConfigurerAdapter,它在配置Jackson和Jaxb时会表现得很奇怪 . 发生在我身上!!!
如果您使用ConfigurationSupport类,那么在尝试提供静态资源时,请记住麻烦的麻烦,因为它不起作用 .