Spring MVC disables caching for .js resource files

We have a bunch of .js files in a web application that are not located under the same directory. The user interface was developed separately, and it took quite a while to reorganize it to have all * .js files in one place.

The problem is that these files are very heavily cached by browsers, and this creates a lot of problems with every update of the application. And we decided to disable caching for these files.

So, the * .js files were included in the servlet mapping:

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

I tried using mvc: resources, but it does not handle url masks as follows:

<mvc:resources mapping="*.js" location="*.js" cache-period="0"/>

This does not work, and I have a 404 answer when I try to access the js file.

I also tried mvc: interceptor:

    <mvc:interceptor>
        <mvc:mapping path="*.js"/>
        <bean id="webJSContentInterceptor"
              class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0"/>
            <property name="useExpiresHeader" value="true"/>
            <property name="useCacheControlHeader" value="true"/>
            <property name="useCacheControlNoStore" value="true"/>
        </bean>
    </mvc:interceptor>

This also results in a 404 error.

Is it possible?

+5
3

. :

  • , *.js .
  • - JavaScript. JS .
+2

, , .

ResourceHttpRequestHandlers (Spring 3.2 +)

<mvc:resources>, :

  • location , ( , webapp, , webJAR...)

, :

src/main/webapp/static/
  |- js/
  |- js/lib/jquery.js
  |- js/main.js
  |- css/style.css

:

<mvc:resources mapping="/**" location="/static/"/>

: cache-period .

(Spring 4.1 +)

Spring 4.1, , .

, JS CSS :

<mvc:resources mapping="/**" location="/static/"/>
  <mvc:resource-chain resource-cache="true">
    <mvc:resolvers>
      <mvc:version-resolver>
        <mvc:content-version-strategy patterns="/**"/>
      </mvc:version-resolver>
    </mvc:resolvers>
  </mvc:resource-chain>
</mvc:resources>

, ( ) javaconfig:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

  VersionResourceResolver versionResolver = new VersionResourceResolver()
    .addContentVersionStrategy("/**");

  registry.addResourceHandler("/**")
    .addResourceLocations(/static/)
    .resourceChain(true).addResolver(versionResolver);
}

.

+6

, JS . , , . , , JS.

. , .

0
source

All Articles