VOOZH about

URL: https://www.javacodegeeks.com/2013/08/spring-mvc-resources.html

โ‡ฑ 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.
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

๐Ÿ‘ Photo of Alexey Zvolinskiy
Alexey Zvolinskiy
August 19th, 2013Last Updated: August 20th, 2013
4 479 1 minute read

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.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

4 Comments
Oldest
Newest Most Voted
Sourabh
12 years ago

thanq .. this article helps me a lot :)

0
Reply
Karl Harshman
12 years ago

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?

0
Reply
user
11 years ago

not working

0
Reply
user
11 years ago

not working for me

0
Reply
Back to top button
Close
wpDiscuz