19

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 Answers 3

23

try (with changing the values)

<mvc:resources mapping="/static/**" location="/public-resources/" 
       cache-period="31556926"/>
<mvc:annotation-driven/>

You can also use an interceptor:

<mvc:interceptors>
   <mvc:interceptor>
    <mvc:mapping path="/static/*"/>
    <bean id="webContentInterceptor" 
         class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="31556926"/>
        <property name="useExpiresHeader" value="true"/>
        <property name="useCacheControlHeader" value="true"/>
        <property name="useCacheControlNoStore" value="true"/>
    </bean>
   </mvc:interceptor>
</mvc:interceptors>

See the MVC docs

Sign up to request clarification or add additional context in comments.

4 Comments

A tiny correction, the correct tag is "mvc:mapping" instead of "mapping, i.e., correct complete tag is : <mvc:mapping path="/static/*"/>
which should i use? i am currently in a situation where I've got both, and wonder if both are needed. Seems like I should choose one or the other. are there any benefits using mvc:resources element?
In case of 304 also client send a HTTP request to server then how it is benefiting
@Manish The client will still send a HTTP Request to the server, but the server will reply with a tiny 304 Response instead of transferring your potentially large files (e.g. images, css, js...) back with a 200 Response. In other words, number of requests/responses is the same, but the response may be significantly smaller in size.
1

If Spring 3.0 is being used, <mvc:resources> is one way to implement caching of static resources. This link has some documentation.

Comments

0

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());
    }

    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.