Not long time ago I faced the need of handling static resources with Spring, and I came up with this presentation from Spring Framework committers Brian Clozel and Rossen Stoyanchev.

In the video Brian and Rossen explain how they have developed a new way of handling this type of files and propose from version 4.1 of Spring a very easy and simple way of implement this idea.

In the presentation they explain how they used this in a project called Sagan, which it turns out to be the new Spring’s home page.

Also in this presentation they try to explain how to use this new feature by referring to a much more simple project, based in two parts, a client part, where they handle all the js,css and other static resources, and a server side, where they implement Spring’s part.

I wanted to go one more step in terms of simplifying this demonstration and I’ve created a little boilerplate project with Spring MVC, Spring Boot and a jsp that uses bootstrap and jquery, and in the Spring’s side I use this new implementation to handle my static resources.

There are basically two main parts that we have to take into account in the project.

  1. In the jsp presentation, I handle my resources by using a jstl core tag, for Spring to notice and be aware of them.
<head>
   <link href="<c:url value="resources/css/bootstrap.min.css"/>" rel="stylesheet">
   <script type="text/javascript" src="<c:url value="resources/js/jquery-2.1.3.min.js"/>"></script>
</head>
  1. In the server side, when we configure our files, we extend the WebMvcConfigurerAdapter and we override the addResourceHandlers method to basically tell Spring where to find our resources, how much time we want the resources to be cached in the client and which type of resolvers and transformers we may want to use.
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/resources/**")
           .addResourceLocations("/resources/")
           .setCachePeriod(10)
           .resourceChain(true)
           .addResolver(new VersionResourceResolver());
}

As of version 4.1.6 of Spring, several things have changed and I have noticed that even classes that I have used in version 4.1.1 don’t exist anymore in the current version, so I have the feeling maybe more things will change in the future about this, but there are many great features in this new part of Spring, having even the possibility of versioning our resources (maybe in another post) and control when and how we want our clients to refresh their versions of our resources.

Again, I encourage you to watch Brian & Rossen’s presentation, where they explain this concept much better than I do.

github_24pxWatch / Download this demo in Github