Alexey Zvolinskiy

About Alexey Zvolinskiy

Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

Spring MVC: Resources

One of the most frequent questions which I receive from my blog readers is how to use css and javascript files in application with Spring MVC. So it’s a good opportunity to write an article about usage of resources in Spring MVC. As usually I will use java based configuration approach.Spring-MVC-Resources

In a nowadays it’s hard to imagine web-application which doesn’t has css and javascript files. In what way Spring MVC can deal with them? Where to place these files in a dynamic web project? How to get access to static resources? I will try to explain all this in a few minutes.
 
 
Firstly you need to have some css or javascript file which you want to plug into a project. In my example I’m going to use a main.css file:

Spring-MVC-resources (1)
Look carefully where I have placed the file (src\main\webapp\resources\css\main.css). After this I can continue with java configuration file.

@Configuration
@EnableWebMvc
...
public class WebAppConfig extends WebMvcConfigurerAdapter {

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
	}
...

Above you can see the code snippet which shows how to modify a configuration class to make resources available. In our case a role of resources plays just one main.css file. Let’s consider what’s going on in the following code:

...
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
	}
...

Here I declare a mapping of src\main\webapp\resources folder and all its content to a resource location value /resources/

After this manipulations you can access css and javascript files in Spring MVC. Here are some example of usage:

If I want to apply main.css to url: www.mysite.com/index.html, I need to use following construction in HTML code:

< link href="resources/css/main.css" rel="stylesheet" type="text/css"/ >

If I want to apply main.css to url: www.mysite.com/some/location.html, I need to use following construction in HTML code:

< link href="../resources/css/main.css" rel="stylesheet" type="text/css"/ >

These two examples are clear enough to understand how to work with Spring MVC resources. In the following posts I will demonstrate resource usage in Spring MVC project.

 

Reference: Spring MVC: Resources from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.
Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

2 Responses to "Spring MVC: Resources"

  1. Sourabh says:

    thanq .. this article helps me a lot :)

  2. Karl Harshman says:

    Thanks but your following sentence is very vague:

    “After this I can continue with java configuration file.”

    Which configuration file? Where is it located? What does it configure?

Leave a Reply


eight − 7 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books