Techno Techno - 3 years ago 173
Java Question

how to add RequestMappingHandlerMapping and ResourceHandlers to a springMVC configuration class

I have a spring MVC configuration class like this:

@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/");
}


}


I have an issue with maping URLs with a trailing slash,similar to this. so i want to add
RequestMappingHandlerMapping
class, but based on the instruction i get there i need to extend
WebMvcConfigurationSupport
class and implement
requestMappingHandlerMapping()
method, but unfortunatly i have already extended WebMvcConfigurationSupport class for resource's mapping. Is there any way i could add requiest mapping handler to my class?

NOTE: i am using Spring version 3.1.1.RELEASE

Answer Source

I didn't get from your question why you can't use WebMvcConfigurationSupport. If by what you mentioned "...unfortunatly i have already extended WebMvcConfigurationSupport class for resource's mapping..." you rather meant that you've already extended the WebMvcConfigurerAdapter, you should be aware that WebMvcConfigurationSupport exposes exactly the same method.

Anyways, following should be a working java config for Spring MVC 3.1 version

@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/");
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download