Spring MVC Tutorial

This is a simple Spring MVC tutorial. In this post, we will learn as how to set up a Spring MVC project. We will perform the setup using following options

  1. XML based Configurations
  2. Java-based Configurations
  3. Spring Boot


1. DispatcherServlet 

Spring MVC, like many other web frameworks, is designed around the front controller pattern where a central Servlet, the DispatcherServlet, provides a shared algorithm for request processing while actual work is performed by configurable, delegate components.

We have the options to provide these configurations using XML or Java based configurations.


1.1 The web.xml

Below is an example of web.xml configuration to register and initialize the DispatcherServlet.

<?xml version="1.0" encoding="UTF-8"?>
         <param-value />


1.2 The web.xml

Below is an example of the Java configuration that registers and initializes the DispatcherServlet.

public class DemoWebApplicationInitializer implements WebApplicationInitializer {

 public void onStartup(ServletContext servletCxt) {

  // Load Spring web application configuration
  AnnotationConfigWebApplicationContext ac = new AnnotationConfigWebApplicationContext();

  // Create and register the DispatcherServlet
  DispatcherServlet servlet = new DispatcherServlet(ac);
  ServletRegistration.Dynamic registration = servletCxt.addServlet("spring-mvc-app", servlet);

Read our article to get more detail about the multiple approaches to configure a Spring MVC project


2. Spring MVC Configurations

Let’s have a look at some of the configurations points required for Spring MVC project setup.


2.1 View Resolvers

public class AppConfig implements WebMvcConfigurer {

 public ViewResolver getViewResolver() {
  InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  return resolver;

Alternatively to the Java configuration above, we can also use a purely XML configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    <context:component-scan base-package="com.javadevjournal" />    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
        <property name="suffix">


2.1 View Technologies

The use of view technologies in Spring MVC is pluggable, whether you decide to use Thymeleaf, Groovy Markup Templates, JSPs, or other, is primarily a matter of a configuration change. To register view technology of choice, we need to register correct ViewResolver.

public class AppConfig implements WebMvcConfigurer {

 public void configureViewResolvers(ViewResolverRegistry registry) {

 // Configure FreeMarker...
 public FreeMarkerConfigurer freeMarkerConfigurer() {
  FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
  return configurer;

Alternatively to the Java configuration above, we can also use a purely XML configuration

<!-- Configure FreeMarker... -->
    <mvc:template-loader-path location="/WEB-INF/freemarker"/>


3. Spring MVC Configurations

The view technologies in Spring MVC is pluggable. We can use JSP, Thymeleaf or Freemarker to render the view.


3.1 JSP View

Let’s create a simple HTML(greeting.jsp) for our view and store it under the /WEB-INF directory.

    <title>Getting Started: Serving MVC Project Structure</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <p>Welcome to the Spring MVC project</p>


3.1 Thymeleaf

Thymeleaf is modern server-side Java template engine that emphasizes natural HTML templates. For more details, refer to our existing article


4. Spring MVC and Spring Boot

If you are starting your project, Spring Boot should be your default choice to build Spring MVC application. Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just” run.


4.1 Spring Boot Starters

When we start with the Spring Boot, one of the fundamental questions which come to our mind is why do we need Spring Boot Starters? or how these starters will help me in my application?

With Spring Boot Starters, bootstrapping our Spring-MVC web application is really straightforward, We need to include spring-boot-starter-web starter in our pom.xml.


Note: For more details on starters, refer to our existing article on Spring Boot Starters.

Please read following articles for building Spring MVC web application using Spring Boot.


5. Spring MVC Controller

The controller is responsible for handling HTTP request in Spring MVC application. Use the @Controller annotation to mark a controller.

public class GreetingController {

    public String greeting(final Model model){
        model.addAttribute("message", "Welcome to our sample controller");
        return "greeting";

Let’s try to understand different features of our MVC Controller.

  • @Controller annotation registers our class as Controller with Spring.
  • @RequestMapping("/greeting") will ensure that our class will process any request mapped to /greeting URI.
  • Our greeting method is annotated with @GetMapping which means this method will be called when a GET request will be sent to /greeting URI.
  • Once greeting method is called, it is saving a message in the Model class and returning a view (in our example view name is “greeting”).
  • In our viewResolver, we configured following properties
    • prefix = /WEB-INF/view/
    • suffix = .jsp
  • ViewResolver will try to resolve view information sent from our controller by combining prefix and suffix configured in our application.
  • Final information about the view will be /WEB-INF/view/greeting.jsp. Spring will try to find greeting.jsp in the /WEB-INF/view/ which will be used to display output on the frontend.



In this Spring MVC tutorial, We created a simple web application using traditional XML based configuration and using Java configuration. In the last part of this post, we learned how to create a Spring MVC project using Spring Boot.


Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

2 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors

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

newest oldest most voted
Notify of
Spring MVC @ModelAttribute Annotation | Java Development Journal

[…] attribute, exposed to a web view. Supported for controller classes with @RequestMapping methods. Spring MVC will invoke all methods with @ModelAttribute annotation before any handler method executed by the […]

Uploading A Files with Spring MVC | Java Development Journal

[…] this article of Spring MVC, We are going to cover the uploading a file in Spring MVC.File Uploading is a very common task in […]