I have been trying to add caching headers to my webapp's static content with no luck, I am trying to set a far future date for all my static contents in my webapp.
I created a Filter servlet to add a desired HTTP header for all the static response with file extensions provided in the web.xml , while debugging I noticed that all the headers did get set with the appropriate HTTP headers inside my Filter servlet class but were getting overridden by the Struts2 FilterDispacher to be the default period of caching provided by the Struts2 framework which only caches static contents for a day.
While going throw the Struts2 source for FilterDispacher.java , I can see the following code flow inside the doFilter() method of FilterDispacher.java
serveStatic in the above code is a StrutsConstant "struts.serve.static=true" in default.properties , I tried making it false in my struts.properties file hoping to make struts2 not to overwrite my static response headers (the ones I set in my personal Filter class), that didn't go well , all the JSP pages were served without static contents .
I am not using StrutsConstant - serveStaticBrowserCache("struts.serve.static.browserCache") as it puts the same 1 day expires header if 'True' or puts 'no-cache' is false.
My static contents are in resource path '/struts' eg. /struts/imgs/fav.png (or) /struts/css/mainstyle.css etc. for which I am trying to set a far future expires date.
I want to know how to tell struts not to put default headers for static response and if that not possible ,
How to make struts accept a external expires duration(eg. 90 days) as FilterDispacher.java by default assumes 1 days as the duration for caching static contents.
Calling my Filter after FilterDispacher in web.xml does not work either , I am not sure if that's possible
Please advice how I can manipulate my response headers for my static contents in my webapp to set a far future date,
Any help/response to solve this problem is appreciated ,
Prakash please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it...
I have found a solution that works for me to solve the caching issue , I dont know if this is the only way but it sure does work for me,
I used @Override annotation to override the default struts FilterDispacher's findStaticResource method , this is the method which was putting 1 day of caching for all the static resources passing through struts.
I override this method to replace the logic where it puts 1 day of caching for static content and replaced it with my desired days of caching.
In the original findStaticResource method it was like the following , which was caching static contents for 1 day , above override fixed this problem,
// set heading information for caching static content
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));