How to handle static content in Spring MVC?

前端 未结 23 2846
春和景丽
春和景丽 2020-11-22 03:28

I am developing a webapp using Spring MVC 3 and have the DispatcherServlet catching all requests to \'/\' like so (web.xml):

  
          


        
23条回答
  •  滥情空心
    2020-11-22 03:50

    For java based spring configuration you can use the following

    Using ResourceHandlerRegistry which stores registrations of resource handlers for serving static resources.

    More Info @ WebMvcConfigurerAdapter which defines callback methods to customize the Java-based configuration for Spring MVC enabled via @EnableWebMvc.

    @EnableWebMvc
    @Configurable
    @ComponentScan("package.to.scan")
    public class WebConfigurer extends WebMvcConfigurerAdapter {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static_resource_path/*.jpg").addResourceLocations("server_destination_path");
    
        }
    

提交回复
热议问题