Spring Boot - Creating web application using Spring MVC

Posted on July 29, 2017

In this post, I will show you how to create and bootstrap a Spring MVC web application using the Spring Boot framework. We will use the JSP as view for our web application.

Tools and technologies used for this application are-

  • Spring Boot 1.5.4.RELEASE
  • Spring WebMVC 4.3.9.RELEASE
  • Tomcat Embedded 8.5
  • JavaSE 1.8
  • Maven 3.3.9
  • Eclipse Neon.3

Project Structure 

Review the spring boot project structure.


Related - How to create a maven project in eclipse IDE.

Jar dependencies

To create and run a Spring MVC web application in spring boot, you need to add the spring-boot-starter dependency in your pom.xml file. 


Here is the complete pom.xml file.


<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">

    <!-- JSTL tag lib -->


    <!-- Tomcat for JSP rendering -->



Controller class

Create a HelloController class under com.boraji.tutorial.springboot.controller package and write the following code in it.


package com.boraji.tutorial.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

public class HelloController {

   public String index() {
      return "index";

   public String sayHello(@RequestParam("name") String name, Model model) {
      model.addAttribute("name", name);
      return "hello";

JSP Views

Create index.jsp and hello.jsp  files under src/main/webapp/WEB-INF/views folder as shown in the project structure.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<!-- Static content -->
<link rel="stylesheet" href="/resources/css/style.css">
<script type="text/javascript" src="/resources/js/app.js"></script>

<title>Spring Boot</title>
  <h1>Spring Boot - MVC web application example</h1>

  <div class="form">
    <form action="hello" method="post" onsubmit="return validate()">
          <td>Enter Your name</td>
          <td><input id="name" name="name"></td>
          <td><input type="submit" value="Submit"></td>



<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring Boot</title>
  <h1>Spring Boot - MVC web application example</h1>

  <h2>Your name is ${name}</h2>



Static web resources

By default Spring Boot serves the static web content from the /static or /public or /resources or /META-INF/resources folder in a classpath. In this example, we will put all static resources such as  stylesheet, JavaScript files under /static folder.

Create a stylesheet file under src/main/resources/static/css folder and write the following code in it.


.form {
	background-color: #efefef;
	width: 400px;
	height: 50px;
	border-radius: 7px;
	padding: 20px;

Now, create a javascript file under src/main/resources/static/js folder and write the following code in it.


function validate() {
	var name = document.getElementById("name").value;
	if (name == '') {
		alert('Please enter a valid name.');
		return false;
	} else {
		return true;

Application properties

Spring boot load the properties from application.properties and add them to the Spring Environment. You can set the properties related to the Spring MVC or static web content in application.properties file.

Create an application.properties file under src/main/resources source folder and write the following properties in it.


spring.mvc.view.prefix = /WEB-INF/views/
spring.mvc.view.suffix = .jsp

Note - spring.mvc.static-path-pattern=/resources/** will map the classpath:/static/css/style.css to /resources/css/style.css. Similarly, the classpath:/static/js/app.js  will be mapped to /resources/js/app.js. You can use these static resources in jsp as follows.

<link rel="stylesheet" href="/resources/css/style.css">
<script type="text/javascript" src="/resources/js/app.js"></script>

Main class

To bootstrap spring MVC we application using Spring Boot, create a main class annotated with @SpringBootApplication annotation as follows.


package com.boraji.tutorial.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class MainApp {
   public static void main(String[] args) {
      SpringApplication.run(MainApp.class, args);


Run application

Run the MainApp.java class as Java application i.e. go to Run→ Run as → Java Application

You can use the mvn spring-boot:run command to run the spring boot application.

After executing the main class, the console output will look like as follow.

Now, enter the http://localhost:8080 in browser's address and enter the your name in input field.


Now, enter the http://localhost:8080 in browser's address and enter the your name in input field.


Click on submit button and see the result page.


Packaging Spring Boot application as an executable war

You need to package the Spring boot application as war instead of jar, if you are using an embedded servlet container. There are some limitations in the JSP support. Read more..

You can use the following maven command to package your spring boot application as war.

mvn clean package

Running executable war from command line

Use the following the following command to run the war from the CMD.

>java -jar spring-boot-web-application-example-0.0.1-SNAPSHOT.war