我有一个像这样的spring MVC配置类:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个问题,使用尾部斜杠来管理URL,类似于此.所以我想添加RequestMappingHandlerMapping类,但基于我到达那里的指令,我需要扩展 WebMvcConfigurationSupport 类和实现requestMappingHandlerMapping()方法,但不幸的是我已经扩展了WebMvcConfigurationSupport类的资源映射.有什么办法可以为我的班级添加requiest映射处理程序吗?
注意:我使用的是Spring 3.1.1.RELEASE版
我没有从你的问题中得到你为什么不能使用WebMvcConfigurationSupport.如果通过你提到的"......不幸的是我已经扩展WebMvcConfigurationSupport了资源的映射类......"你宁愿意味着你已经扩展了它WebMvcConfigurerAdapter,你应该知道WebMvcConfigurationSupport暴露完全相同的方法.
无论如何,以下应该是Spring MVC 3.1版本的工作java配置
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {
@Override
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();
hm.setUseSuffixPatternMatch(false);
return hm;
}
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
}
}
Run Code Online (Sandbox Code Playgroud)
使用WebMvcConfigurationSupport覆盖requestMappingHandlerMapping可能会关闭Spring Boot的默认配置。更好的方法是将WebMvcRegistrations用作
@Configuration
static class CustomRequestMappingHandlerMapping {
@Bean
public WebMvcRegistrationsAdapter webMvcRegistrationsHandlerMapping() {
return new WebMvcRegistrationsAdapter() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new MyRequestMappingHandlerMapping();
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
从 Spring Boot 2.0.0 开始,您可以直接使用 WebMvcRegistrations
@Configuration
public class WebMvcConfig {
@Bean
public WebMvcRegistrations webMvcRegistrationsHandlerMapping() {
return new WebMvcRegistrations() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new CustomRequestMappingHandlerMapping();
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6427 次 |
| 最近记录: |