Exclude particular file (static resource)

2019-08-29 20:36发布

问题:

I have a spring boot application and below is the code for registrations of resource handlers:

@Configuration    
public class MyAppConfig extends WebMvcConfigurerAdapter {
       ...
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/js/**")         
            .addResourceLocations("/resources/js/").setCachePeriod(31536000);
          //in same directory where all the js files placed,
          //I want to cache all the files in that directory
          //except for one file i.e. prop.js
          /*registry.addResourceHandler("/resources/js/prop.js")
           .addResourceLocations("/resources/js/").setCachePeriod(0);*/
       }
       ...
    }

There are certain things I need to understand, viz.
1. What does the parameter of addResourceHandler mean?
2. What does the parameter of addResourceLocations mean?
3. Is there any way to explicitly prevent prop.js from being cached?

There are may files at the same level where prop.js is, I want all other files to be cached.

回答1:

  1. In the addResourceHandler method, you specify URL, or URL pattern.
  2. In the addResourceLocations, you specify a valid directory actually containing (or possibly many directories separated by commas) the static resources.

So in the end, you made a simple mapping between URL ans directories on your web-server.

  1. if you want to disable caching only for the file prop.js, try this :

    registry
        .addResourceHandler("/resources/js/**/prop.js")
        .addResourceLocations("/resources/js/")
        .setCachePeriod(0);
    registry
        .addResourceHandler("/resources/js/**")
        .addResourceLocations("/resources/js/")
        .setCachePeriod(31536000);