In this tutorial we will learn how Spring Boot greatly simplify the creation of Web applications by using its auto-configuration, comparing with the old Spring approach.

The Spring Framework used as basis for developing Web applications the MVC (Model View Controller) pattern which greatly simplifies application development. Let’s have a look how you could build a standard Spring MVC application starting from a simple Maven archetype:

$ mvn archetype:create -DgroupId=com.example -DartifactId=simple-mvc-app -DarchetypeArtifactId=maven-archetype-webapp

After some adjustments to the pom.xml file we will have this project file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.example</groupId>
   <artifactId>simple-mvc-app</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>war</packaging>
   <properties>
      <!-- Generic properties -->
      <java.version>1.8</java.version>
      <!-- Web -->
      <jsp.version>2.2</jsp.version>
      <jstl.version>1.2</jstl.version>
      <servlet.version>2.5</servlet.version>
      <!-- Spring -->
      <spring-framework.version>3.2.3.RELEASE</spring-framework.version>
   </properties>
   <dependencies>
      <!-- Spring MVC -->
      <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-webmvc</artifactId>
         <version>${spring-framework.version}</version>
      </dependency>

      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>jstl</artifactId>
         <version>${jstl.version}</version>
      </dependency>
      <dependency>
         <groupId>javax.servlet</groupId>
         <artifactId>servlet-api</artifactId>
         <version>${servlet.version}</version>
         <scope>provided</scope>
      </dependency>
      <dependency>
         <groupId>javax.servlet.jsp</groupId>
         <artifactId>jsp-api</artifactId>
         <version>${jsp.version}</version>
         <scope>provided</scope>
      </dependency>
   </dependencies>
   <build>
      <finalName>simple-mvc-app</finalName>
   </build>
</project>

As you can see, we are using Spring MVC version 3.2.3.RELEASE and some other dependencies like the tag libraries. Right now the Spring MVC is in its 5.x version. Next, let’s look at web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
   <display-name>simple-web-spring-app</display-name>
   <servlet>
      <servlet-name>dispatcherServlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/mvc-config.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>dispatcherServlet</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>
</web-app>

As you can see, we are using a Servlet declaration that adds the org.springframework. web.servlet.DispatcherServlet class that is the main dispatcher that will trigger the MVC pattern. As an additional declaration, the tag will look for an XML configuration file, in this case WEB-INF/mvc-config.xml . This file is a Spring context configuration.

Finally, take a look at the Spring configuration contained in src/main/webapp/WEB-INF/mvc-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   <bean name="/showMessage.html" class="com.apress.spring.SimpleController" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/view/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

This is the standard Spring configuration where you define your bean that will be instantiate the Spring container. Within this configuration file, there is a bean named /showMessage.html that is pointing to the com.example.SimpleController Java class. This peculiar declaration is the URL that will map to the class to be executed when there is a request to the /showMessage.html URL. There is also one more bean declaration that you need to add to define your views. In this case, every view will placed in the /WEB-INF/view folder and every page will have the .jsp extension. Now let's take a look at the SimpleController.java class:

package com.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class SimpleController extends AbstractController {
 @Override
 protected ModelAndView handleRequestInternal(HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  ModelAndView model = new ModelAndView("showMessage");
  model.addObject("message", "Spring MVC Web Application");
  return model;
 }
}

This class extends the org.springframework.web.servlet.mvc.AbstractControlle class that has all the logic to manage your request ( /showMessage.html ). In the handleRequestInternal method we will return a ModelAndView instance that contains the information of what view to display.

Finally, let's compose the view, named "showMessage.jsp" as declared in the Controller class: (This file will be located in /WEB-INF/view/showMessage.jsp)

<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
   <head>
      <meta charset="utf-8" />
      <title>Welcome</title>
   </head>
   <body>
      <h2>${message}</h2>
   </body>
</html>

When you deploy this application, Spring will render the "Spring MVC Web Application" message.

Package your applicaiton as follows:

$ mvn clean package

This will create the simple-mvc-app.war application that can be deployed on a Web Server/Application server and tested with:

http://localhost:8080/simple-mvc-app/showMessage.html URL and it will show the message "Spring MVC Web Application".

Spring MVC 2.5 and above

In the above example, you might have noticed that we used an old approach to Spring MVC applications. As a matter of fact from the version 2.5 of Spring MVC you can add annotations to avoid extending from other classes, thus simplifying the mapping. Consider the following SimpleController class:

package com.example;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/showMessage.html")
public class HelloWorldController {
 @RequestMapping(method = RequestMethod.GET)
 public ModelAndView helloWorld() {
  ModelAndView model = new ModelAndView("showMessage");
  model.addObject("message", "Spring MVC Web App with annotations");
  return model;
 }
}

The changes required in src/main/webapp/WEB-INF/mvc-config.xml are:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
   <context:component-scan base-package="com.example" />
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/view/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>

As you can see from the above example, now we are using a tag which tells the Spring engine to scan for all classes marked with that package. In this example, it will find the SimpleController class as it has been tagged with the @Controller annotation. Also, you might have noticed that all bean definitions have been removed from the configuration that is now handled by the SimpleController class.

Spring Boot Web Applications

Now it's time to see how Spring Boot can help you in creating Web application using the MVC pattern. We will use the Spring CLI to bootstrap a simple Web application template therefore open a terminal, create a folder ( simple-mvc-spring-boot ), and execute the following command:

$ mkdir simple-mvc-spring-boot
$ cd simple-mvc-spring-boot
$ spring init -d=web -g=com.example -a=simple-mvc-spring-boot --package-name=com.example -name=simple-mvc-spring-boot -x

This command will create a template for your Spring Boot web application.

• The -d=web tells the CLI to include the web starter

• The -g=com.example is the groupId

• The -a=simple-mvc-spring-boot is the artifactId

• The --package-name=com.example is the base package name for the classes

• The -name=simple-mvc-spring-boot is the name of the application

• The -x will extract the template in the current directory; if you omit the extract option, you will find a simple-mvc-spring-boot.zip file

Your file structure should be similar to the following one:

├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── SimpleMvcSpringBootApplication.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── example
                    └── SimpleMvcSpringBootApplicationTests.java

Now open the file SimpleMvcSpringBootApplication.java and modify it to look the same as

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@SpringBootApplication
public class SimpleMvcSpringBootApplication {
 @RequestMapping("/hello.html")
 public String index() {
  return "Hello World Spring Boot MVC!";
 }
 public static void main(String[] args) {
  SpringApplication.run(SimpleMvcSpringBootApplication.class, args);
 }
}

In this modified application we have marked the rest controller with the @RestController annotation and defined an index method marked with @RequestMapping . The purpose of this method is to accept all incoming requests to the /hello.html URL. To run it, just execute the following command:

$ ./mvnw spring-boot:run

This command will run the application, so check with the browser at the default Web server address and port for http://localhost:8080/hello.html URL. You will see the message: "Hello World Spring Boot MVC!".

Spring Boot without Maven

In the above example, we have created a Spring Boot application from a template, by invoking the "spring init" command. You can even create a Maven-less application which just uses a minimal @RequestController and @RequestMapping. By using groovy as language, you won't even need package declarations for the standard types:

@RestController
class WebApp{
	@RequestMapping("/hello.html")
	String greetings(){
		"Spring Boot MVC is easier"
	}
}

You can just run the application with the following command:

$ spring run app.groovy

Now check with the browser at: http://localhost:8080/hello.html . As you can see we got rid of maven, web.xml, bean declaration and any kind of configuration

Conclusion

In this tutorial we have seen the evolution of Spring MVC applications and how Spring Boot greatly simplifies the design and development of Web applications using this awesome pattern. Check out for more Spring Boot MVC tutorials:

REST services in Spring Boot with MVC and JAX-RS

Spring Boot MVC example

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials