Spring Boot + Thymeleaf

This tutorial will build a simple hello world spring boot web application with thymeleaf which take about 15 min.

Technologies

  • Gradle 5.2.1
  • Spring Boot
  • Thymeleaf

Build with Gradle

The project structure will look like below

Inside the build.gradle file, we will define org.springframework.boot:spring-boot-starter-web and org.springframework.boot:spring-boot-starter-thymeleaf for the project implementation.

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/5.2.1/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id "io.spring.dependency-management" version "1.0.7.RELEASE"

    // Apply the application plugin to add support for building an application
    id 'application'
    id 'eclipse'
}

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    implementation 'com.google.guava:guava:27.0.1-jre'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'my.cychew.App'

Java Classes

Now we go to our main class file, in my case will be App.java. We will add in @SpringBootApplication for spring to auto configure in default way and then inside the main method we add in SpringApplication.run(App.class, args). This will ask the program to start the application for us.

/*
 * This Java source file was generated by the Gradle 'init' task.
 */
package my.cychew;

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

@SpringBootApplication
public class App {

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

In the controller class MyController, we create a simple GET request page which mapped to path “/hello” and it will use welcome.html page to display.

package my.cychew;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MyController {

	@GetMapping("/hello")
	public String helloWorld() {

		return "welcome";
	}
}

welcome.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Welcome</title>
</head>
<body>
	<h1>Hello World</h1>
</body>
</html>

Finally, we make use gradle bootrun command to start our web application, and use browser to access the page, localhost:8080/hello

Spring Boot + Thymeleaf
Tagged on:         

Leave a Reply

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