Spring MVC — Configuring to render Static Contents

Syed Hasan
2 min readFeb 13, 2020

--

In this post I will try to demonstrate how to render static contents from an Spring MVC application. If you already know how to configure and run very minimum Spring Application, then I assume, it will be helpful for you. Besides, here you can find the source code of a very minimum Spring application. To follow along this course download the source code and this portfolio template files.

We will start by adding our configuration. In our previous post, I wrote Servlet configuration where I mapped our JSP pages. I also mentioned that WebMvcConfigurer provides us many methods to override. For rendering static resources we will override addResourceHandlers(ResourceHandlerRegistry registry) method.

  1. Configuration
// Configuration to render STATIC CONTENTS (IMAGE, CSS, JS)
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

// Register resource handler for -

// IMAGES
registry.addResourceHandler("/images/**") // Relative paths
.addResourceLocations("/WEB-INF/resources/images/") // Actual resource locations
.setCachePeriod(999999999); // Cache period

// CSS
registry
.addResourceHandler("/css/**")
.addResourceLocations("/WEB-INF/resources/css/")
.setCachePeriod(999999999);

// JAVASCRIPT
registry
.addResourceHandler("/js/**")
.addResourceLocations("/WEB-INF/resources/js/")
.setCachePeriod(999999999);

// Other template resource vendor files
registry
.addResourceHandler("/vendor/**")
.addResourceLocations("/WEB-INF/resources/vendor/")
.setCachePeriod(999999999);
}

Take a look at the above code. From the registry object, we can find several methods. The methods we have called are described below.

a) addResourceHandler(String s)

This method receives the relative path named to be defined. The relative paths we are providing here will be used to get our resources in JSP page. Following is an example given.

<link href="${pageContext.request.contextPath}/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

b) addResourceLocations(String s)

This method receives the actual path where it will search the resources for. If we don’t have this directory, we will have to create by our own.

c) setCachePeriod(int i)

This method receives as integer value. It denotes that our static resources will be cached for the provided amount of seconds.

2. Putting static resources in place

We will put our template’s static resources in webapp > WEB-INF > resources directory.

Directory structure

3. Linking resources in page

Now we will link our static resources in pages. The relative paths will be like below.

<!-- For CSS -->
<link href="${pageContext.request.contextPath}/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/resume.min.css" rel="stylesheet">
<!-- For images -->
<img class="img-fluid img-profile rounded-circle mx-auto mb-2" src="${pageContext.request.contextPath}/images/profile.jpg" alt="">
<!-- For Scripts -->
<script src="${pageContext.request.contextPath}/vendor/jquery/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/vendor/bootstrap/js/bootstrap.bundle.min.js"></script><!-- Plugin JavaScript --><script src="${pageContext.request.contextPath}/vendor/jquery-easing/jquery.easing.min.js"></script><!-- Custom scripts for this template --><script src="${pageContext.request.contextPath}/js/resume.min.js"></script>

The full source code can be found here.

--

--

Syed Hasan
Syed Hasan

Written by Syed Hasan

Software Engineer | Back-End Developer | Spring Developer | Cloud Enthusiast

No responses yet