Search code examples
jqueryajaxjspstruts2struts2-jquery-plugin

An "_=13 digits" will be added to js files in Struts 2 jQuery plugin project


In a Struts 2 jQuery plugin project, consider a simple JSP page which includes a JS file in it

<script type="text/javascript"
    src="js/sample.js"></script>

When you look at at actual requested URL ( for example by using Firefox firebug) you see that the an underline with a 13 digit is added to the end of URL:

localhost:8080/js/sample.js?_=1402322518885

when you refresh the page it will look like

localhost:8080/js/sample.js?_=1402322518886
localhost:8080/js/sample.js?_=1402322518887
localhost:8080/js/sample.js?_=1402322518888

You can see this by visiting http://struts.jgeppert.com/struts2-jquery-showcase/index.action. Open firebug network control then Go to widgets/spinner menu and you can see that the globalize.js will be called something like globalize.js?_=1402323154341

This prevent the JS to be cached on client. Do you know what is this and how can we prevent it?! I found that this will not apply to all JS files, as far as I found if your Ajax loaded content has an JS function in it, it will be called with underline plus number


Solution

  • Struts2 jQuery plugin uses jQuery and this feature is related to not caching pages in jQuery. The plugin is configurable to change this feature via attribute ajaxcache of the head tag. From the docs

    Name        ajaxcache       
    Required    false       
    Default     false       
    Evaluated   false       
    Type        Boolean     
    Description If set to false it will force the pages that you request to not be 
                cached by the browser. 
    

    Using the code below in the index.jsp will change not caching default

    <s:if test="%{theme == 'showcase' || theme == null}">
        <sj:head debug="true" compressed="true" jquerytheme="showcase" customBasepath="themes" loadFromGoogle="%{google}" ajaxhistory="%{ajaxhistory}" defaultIndicator="myDefaultIndicator" defaultLoadingText="Please wait ..." ajaxcache="true"/>
    </s:if>
    <s:else>
        <sj:head debug="true" compressed="true" jquerytheme="%{theme}" loadFromGoogle="%{google}" ajaxhistory="%{ajaxhistory}" defaultIndicator="myDefaultIndicator" defaultLoadingText="Please wait ..." ajaxcache="true"/>
    </s:else>