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.

<properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring.version>5.3.14</spring.version>
    <servlets.version>3.1.0</servlets.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>${servlets.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>3.0.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring5</artifactId>
        <version>3.0.14.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>16</source>
                <target>16</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
            <configuration>
                <path>/</path>
            </configuration>
        </plugin>
    </plugins>
</build>

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 {

  @Override
  public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.scan("com.springexamples.demo");
    container.addListener(new ContextLoaderListener(context));

    ServletRegistration.Dynamic dispatcher = container.addServlet("webmvc",
      new DispatcherServlet(context));
    dispatcher.setLoadOnStartup(1);
    dispatcher.addMapping("/");
  }
}

4. Controller and Views

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

@Controller
public class HomeConroller {
  @GetMapping("/home")
  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.

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">

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