Uses Spring Boot Spring MVC

Build your first Spring Boot - Web MVC application

This tutorial shows you how to build a simple Spring Boot Web MVC application. We will use the following tools and technologies:

  • Spring Boot - 2.1.3. RELEASE
  • Java 8
  • Maven
  • Eclipse STS

We will use JSP as the view for our application.

Package structure

The project structure for the Spring MVC web application is shown below:

You can create a simple Spring Boot application with Maven at the following link: Building a Simple Spring Boot Application

Once the simple Spring Boot application is set up, it will act as the backbone for building our first Spring Boot Web MVC application, as shown in the next few sections.

Project dependencies

Add the following dependency in pom.xml to include Spring Web MVC support in our application

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

Here is a full pom.xml for your reference:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Tomcat for JSP rendering --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- JSTL tag lib --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

Main application class

The main class is used to boot a Spring Boot MVC web application. Here is a simple application class that will be used for this sample project:

package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SampleApp {public static void main (String [] args) { (SampleApp.class, args); }}

Controller class

The controller class is used to handle the web requests. Here is a sample controller for the purpose of this project.

There are 4 events involved when sending a request to an application:

  • After receiving an HTTP requestDispatcherServlet advises theHandlerMapping call the appropriate oneController.
  • TheController accepts the request and calls the appropriate service methods based on the GET or POST method used. The service method sets model data based on the defined business logic and returns the view name to theDispatcherServlet.
  • TheDispatcherServlet will take help fromViewResolver to include the defined view (JSP page) for the request.
  • Once the view is complete, theDispatcherServlet Passes the model data to the view (JSP page), which is ultimately rendered by the browser.

For example, read the code in the controller used in the project:

package com.example.demo.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; @Controller public class HelloWebController {@RequestMapping ("/") public String hello () {return "login"; } / * * Accepts the request param named 'name "and passes it to the next view ie, * welcome.jsp * / @PostMapping (" / welcome ") public String welcome (@RequestParam (" name ") String name, Model model) {model.addAttribute ("name", name); // Setting the model data in the view return "welcome"; // Returns the view name to be rendered by the browser, ie, welcome.jsp}}

Static resources

All static content such as CSS, JS and images related to the application is saved under folder in Classpath Resources (usually the classpath location is:)

In this example, all JavaScript files are stored under folder.

All CSS (style sheets) are placed under folder.

// JS function to validate whether user entered the Name or not function validate () {var name = document.getElementById ("name"). Value; if (name == '') {alert ('Please enter a valid name.'); return false; } else {return true; }}

Here is the CSS file used in this project:

/ * Style for User Form * / .form {background-color: # 2adef6; width: 400px; height: 100px; border-radius: 15px; padding: 20px; } / * Style for Table column * / td {padding-top: 20px; padding-bottom: 20px; padding-right: 20px; }

Create a file under Folder and add the following properties:

spring.mvc.view.prefix = / WEB-INF / jsp / spring.mvc.view.suffix = .jsp # Path pattern used for static resources. spring.mvc.static-path-pattern = / resources / **

Note - will map to. Similarly, that is assigned.

You can use these static resources in jsp as follows.

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

JSP views

Create and files undersrc / main / webapp / WEB-INF / jsp Folder as shown in the project structure.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <!-- Static content --> <link rel="stylesheet" href="/de/resources/css/style.css"> <script type="text/javascript" src="/resources/js/int.js"></script> <title>Spring Boot Web MVC Application</title> </head> <body> <h1>A sample Spring Boot - MVC web application</h1> <hr> <div class="form"> <form action="welcome" method="post" onsubmit="return validate()"> <table> <tr> <td>Please specify Your name</td> <td><input id="name" name="name"></td> </tr> <tr> <td></td> <td><input type="submit" value="Submit"></td> <td></td> </tr> </table> </form> </div> </body> </html><%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring Boot</title> </head> <body> <h1>Spring Boot - MVC web application example</h1> <hr> <h2>Welcome back, ${name}</h2> </body> </html>

Run the application

Run the class as a Java application, i.e. go to Run → Run As → Java Application

You should see the output like this:

Now enter the following URL into the browser: http: // localhost: 8080 /

Now enter your name and click Submit. The following welcome page is displayed


In this tutorial we saw how to build a simple Spring Boot MVC web application using the Maven Build Tool and JSP as a view.


Catalog Spring Boot Tutorial