How to enable browser caching of static content(images, css, js) with Tomcat?

前端 未结 3 1206
野性不改
野性不改 2021-02-02 12:38

How to enable browser caching of static content(images, css, js) with Tomcat? Preferable solution will be editingspring MVC config files or web.xml

3条回答
  •  滥情空心
    2021-02-02 13:17

    For those who use Java configuration, you can manage caching parameters using ResourceHandlerRegistry, there is example how do I set up different caching preferences for different content types:

    @Configuration
    @EnableWebMvc
    // ...
    public class WebConfiguration extends WebMvcConfigurerAdapter {
    
        // ...
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
            registry.addResourceHandler("/ui/css/**")
                    .addResourceLocations("classpath:/WEB-INF/css/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS));
    
            registry.addResourceHandler("/ui/js/**")
                    .addResourceLocations("classpath:/WEB-INF/js/")
                    .setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS));
    
            registry.addResourceHandler("/ui/**")
                    .addResourceLocations("classpath:/WEB-INF/")
                    .setCacheControl(CacheControl.noCache());
        }
    
        // ...
    }
    

提交回复
热议问题