Bootstrapping a Spring WebMVC Application

Learn to create a Spring WebMVC application project, including dependencies, initial configurations and deploying the application in the server.

1. Creating Project Skeleton

The simplest way to create the web application skeleton project (necessary files and folder) is using your favorite IDE and following new project creation steps.

We are using Eclipse, and we will use Maven maven-archtype-webapp to generate the project structure.

Launch New Project Wizard
Select Maven Archetype
Fill Maven Coordinates

Now click on Finish button to generate the project.

Generated Project Structure

2. Configure Dependencies

Nest we need to include Spring MebMVC and other needed dependencies in the project. Since it is an MVC application, we are configuring the following dependencies.

  • Spring WebMVC
  • Servlet API
  • Thymeleaf
  • JUnit
  • Tomcat maven plugin

The tomcat maven plugin will help in quickly testing the application in a running server instance.




3. DispatcherServlet Configuration

Servlets 3 specification allows writing the servlets configuration using the Java classes.

WebApplicationInitializer has been used to configure the ServletContext programmatically as opposed to (or possibly in conjunction with) the traditional code web.xml approach.

public class AppInitializer implements WebApplicationInitializer {

  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    container.addListener(new ContextLoaderListener(context));

    ServletRegistration.Dynamic dispatcher = container.addServlet("webmvc",
      new DispatcherServlet(context));

4. Controller and Views

Create a simple MVC controller using @Controller and @GetMapping annotations.

public class HomeConroller {
  public String userForm(Locale locale, Model model) {
    model.addAttribute("message", "Hello World !!");
    return "home";

The message sent from the controller will be rendered using the home.html in the browser.

<html xmlns:th="">

	<title>Getting Started: Spring WebMVC Application</title>
	<meta http-equiv="Content-Type" content="text/html;