Home » Java » Enterprise Java » Spring MVC Tutorial

About Eugen Paraschiv

Eugen Paraschiv

Spring MVC Tutorial

1. Overview and Maven

This is a simple Spring MVC tutorial showing how to set up a Spring MVC project, both with Java based Configuration as well as with XML Configuration.

The Maven artifacts for Spring MVC project are described in the in detail in the Spring MVC dependencies article.

2. The web.xml

This is a simple configuration of the web.xml for a Spring MVC project:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   id="WebApp_ID" version="3.0">

   <display-name>Spring MVC Java Config App</display-name>




We are using Java based Configuration, so we’re using AnnotationConfigWebApplicationContext as the main context class – this accepts @Configuration annotated classes as input. As such, we only need to specify the package where these configuration classes are located, via contextConfigLocation.

To keep this mechanism flexible, multiple packages are also configurable here, simply space delimited:

      <param-value>org.baeldung.spring.web.config org.baeldung.spring.persistence.config</param-value>

This allows more complex projects with multiple modules to manage their own Spring Configuration classes and contribute them to the overall Spring context at runtime.

Finally, the Servlet is mapped to / – meaning it becomes the default Servlet of the application and it will pick up every pattern that doesn’t have another exact match defined by another Servlet.

3. The View Configuration

The Spring MVC Java configuration is simple – it uses the MVC configuration support introduced in Spring 3.1:

public class ClientWebConfig extends WebMvcConfigurerAdapter {

   public void addViewControllers(ViewControllerRegistry registry) {


   public ViewResolver viewResolver() {
      InternalResourceViewResolver bean = new InternalResourceViewResolver();


      return bean;

Very important here is that we can register view controllers that create a direct mapping between the URL and the view name – no need for any Controller between the two now that we’re using Java configuration.

4. The JSP Views

We defined above a basic view controller – sample.html – the corresponding jsp resource is:


      <h1>This is the body of the sample view</h1>	

The JSP based view files are located under the /WEB-INF folder of the project, so they’re only accessible to the Spring infrastructure and not by direct URL access.

5. Conclusion

In this example we configured a simple and functional Spring MVC project, using Java configuration. The implementation of this simple Spring MVC tutorial can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

When the project runs locally, the sample.html can be accessed at:


Reference: Spring MVC Tutorial from our JCG partner Eugen Paraschiv at the baeldung blog.
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
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

Leave a Reply


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

Notify of