1

How can i set js, css, image file expiration in spring web application.I tried <mvc:resources mapping="/resources/**" location="/resources/" cache-period="31556926"/>

but still it send a request from browser and server is responding with 304 means it not using browser cache if i am not wrong. and also at the end it sending a HTTP request also to the server so i dont think this is of any useful purpose.

Am i missing something or there are other was ? Response header

Cache-Control:max-age=31556926, must-revalidate
Date:Wed, 03 Dec 2014 03:17:24 GMT
Expires:Thu, 03 Dec 2015 09:06:11 GMT
Pragma:no-cache
Server:Apache-Coyote/1.1
X-XSS-Protection:1; mode=block
Manish Kumar
  • 10,214
  • 25
  • 77
  • 147
  • Have you checked the response headers to make sure they are being set properly? – Matt Whipple Dec 03 '14 at 02:58
  • The pragma is obviously not helping for any browser that honors it (and suggests that something else is modifying your headers). I'd look at controlling the headers directly in Spring using an appropriate Interceptor: http://stackoverflow.com/questions/1362930/how-do-you-set-cache-headers-in-spring-mvc – Matt Whipple Dec 03 '14 at 03:28
  • i think that is same as setting `cache-period` in `mvc:resources` – Manish Kumar Dec 03 '14 at 10:42
  • It seems as though your headers are getting modified somewhere else in your configuration and the `Pragma` header is being added. That header is often added by interceptors where caching is disabled, and cannot be cleanly removed once added. The expected standard behavior is not taking into account anything else that is modifying the response. – Matt Whipple Dec 03 '14 at 13:00
  • Also "Am i missing something or there are other was ?" I'm guessing that is supposed to be "ways"? and if it is then you seem less than receptive to alternatives. – Matt Whipple Dec 03 '14 at 13:02
  • i not specified any interceptor in my spring config xml nor i am modifying headers anywhere in my app – Manish Kumar Dec 03 '14 at 13:14
  • The header is coming from somewhere, I'd isolate where it is being added and by what. You could jam a servlet filter in or something similar to remove the header but it's normally better to avoid problems than fix them after they exist. – Matt Whipple Dec 03 '14 at 13:18
  • i am using `UrlRewriteFilter` and added that `filter` to my `web.xml` to redirect `example.com` to `www.example.com` – Manish Kumar Dec 03 '14 at 13:23

1 Answers1

0

You may try writing simple filter class for setting the headers:-

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        chain.doFilter(request, response);
         HttpServletResponse res =
              (HttpServletResponse) response;

           String headerName="Cache-Control";
           res.addHeader(headerName,"max-age=31556926");
    }
Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Panther
  • 3,312
  • 9
  • 27
  • 50