Spring Boot Web + Thymeleaf Hello World Example in IntelliJ in 5 Easy Steps


This Tutorial is a quick starter for Spring Web Application using Spring Boot + ThymeLeaf, follow the below 5 easy steps to run your very first Spring Web Application in less than 5 minutes,



Step 1: Setup Spring Web Workspace using Spring Initializr

  • Open IntelliJ IDE,
  • Create New Project,
  • Select Spring Initializr
  • Select the Project SDK (I have selected Java SDK 1.8)
  • Click Next,
  • Select Project Type as Gradle Project
  • Java Version (8 in my case)
    New Spring Boot Project using Initilizr
  • Select Dependencies: Spring Web and Thymeleaf
    Select Dependencies Spring Web and Thymeleaf
  • Click Next
  • Click Finish
  • If IntelliJ prompts click on "Load Gradle Project"


Step 2: Create Sping Web Controller

Under src > main > java com.example.demo, create MyHelloController.java

package com.example.demo;

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

@Controller
public class MyHelloWorldController {

    @GetMapping("/hello-world")
    public String greeting() {
        return "helloWorld";
    }

}


Step 3: Create Thymeleaf HTML Page

Navigate to src > main > resources > templates and create file helloWorld.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Web + Thymeleaf - Hello World Example!</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>


Step 4: Run the Spring Web Application

Navigate to src > main > java com.example.demo and open DemoApplication and Run the main method.



Step 5: View Results on Web Broswer

Now open any Web Brower (Safari, Chrome, Firefox, or Edge) and open URL: http://localhost:8080/hello-world

Hello World - Spring Web and ThymeLeaf Results

Download this project from GitHub: https://github.com/code2care/spring-web-thymeleaf-hello-world-example/tree/main/demo



Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.