permaculture playing cards*
The moose likes Struts and the fly likes Static content caching problem in Struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Static content caching problem in Struts2" Watch "Static content caching problem in Struts2" New topic
Author

Static content caching problem in Struts2

Prakash Saptagiri
Greenhorn

Joined: Feb 16, 2010
Posts: 4
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
(eg.)
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
..
..
<filter-mapping>
<filter-name>MyCacheControlFilter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>


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 ,

-Thanks
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Howdy Prakash, welcome to javaranch.

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Prakash Saptagiri
Greenhorn

Joined: Feb 16, 2010
Posts: 4
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.

(eg.)




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"));
response.setHeader("Date",df.format(cal.getTime())+" GMT");
cal.add(Calendar.DAY_OF_MONTH,1);
response.setHeader("Expires",df.format(cal.getTime())+" GMT");
response.setHeader("Retry-After",df.format(cal.getTime())+" GMT");
response.setHeader("Cache-Control","public");
response.setHeader("Last-Modified",lastModified+" GMT");


-Cheers

 
Don't get me started about those stupid light bulbs.
 
subject: Static content caching problem in Struts2
 
Similar Threads
Struts2 with RAD7
migration to Struts2
Struts2 with RAD7
Struts2 struck with a issue of 404
Setting response headers conditionally