Ashwin Sharma
Ashwin Sharma

Reputation: 117

Avoid caching of js and css files in jsp page

I have searched it a lot on google but didn't get any answer for the same. In order to avoid caching of js and css files we can append a version number after the string like i have done below.

<script type="text/javascript"
    src="<c:url value='/resources/js/Invoice.js?version=1.0'/>">
</script>

But I wish to use some random number instead of 1.0 to avoid caching something like :

<script type="text/javascript"
        src="<c:url value='/resources/js/Invoice.js?version=<%=Math.random()%>' />">
    </script>

But its not working at all. Please help me out

Upvotes: 1

Views: 2097

Answers (2)

VPK
VPK

Reputation: 3090

For storing build numbers I think properties files is the best place. And for your problem, you can get this build number using Spring tag library which can be included like this,

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

Updated

You can then get the build number from properties file which is configured in your ApplicationServlet.xml and finally can use build number as follows,

<spring:message code="buildNumber" var="buildNumber" />
<link rel="stylesheet" href="<c:url value="/resources/css/custom/select2.css?${buildNumber}"/>" type="text/css" />

Your applicationServlet-servlet.xml file must include something like this,

<beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <beans:property name="basenames">
        <beans:list>
            <beans:value>buildNumber</beans:value>
            <beans:value>gui</beans:value>
            <beans:value>message</beans:value>              
        </beans:list>
    </beans:property>
</beans:bean>

Here you can store your build number in separate properties file named, buildNumber.properties and having entry like this, buildNumber=601.

You can change the build number anytime you want to deploy your latest code to the server.

With this you can ensure that your client won't need to clear his own browsers cache whenever a new version of your application has deployed.

Upvotes: 1

Khusboo
Khusboo

Reputation: 117

Please use this key value in your property file, but I am not sure about your scenario, this has worked for me. browser.cache.disabled=true

Upvotes: 0

Related Questions