Spring Boot Web MVC

Spring boot makes easy for us developers to get up and running with a spring mvc project. You add the following dependencies and you are ready to go.


We are using thymeleaf for our views.

Create a controller.

Pay attention to the @Controller annotation which turns this class, UserController, into a spring controller component.

public class UserController {

Create a request handler within @Controller.

In this case we are using a Get request handler. Use the Model class to pass data to you view.

public String home(Model model){
		model.addAttribute("message", "Welcome to my page");
		return "home";

Create a view.

Views are generally located in the templates folder under resources folder. Here we are creating a view for our home page.

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1, shrink-to-fit=no">

<title>Spring Boot Thymeleaf Hello World Example</title>

<link rel="stylesheet" th:href="@{/css/main.css}" />

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"



	<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
		<a class="navbar-brand" href="#">Folau Kaveinga</a>

	<div role="main" class="container">

		<div class="starter-template">
			<h1>Spring Boot Web Thymeleaf Example</h1>
				<span th:text="'Hello, ' + ${message}"></span>

	<!-- /.container -->

<!-- jQuery library -->

<!-- Popper JS -->

<!-- Latest compiled JavaScript -->

Static files

Static files such as css and js files can be served from the static folder under the resources folder.

For demo purposes, let create a main css file to style our home page.

body {
  padding-top: 5rem;
.starter-template {
  padding: 3rem 1.5rem;
  text-align: center;



If you start your server and go to /home, your should see the home page being displayed.

Source code on Github

Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!


Leave a Reply

Your email address will not be published. Required fields are marked *