Spring Boot Thymeleaf Hello World

Spring Boot Thymeleaf Hello World

Directory Structure

pom.xml file

Modify auto created POM file. We need to add parent in pom to make it a spring boot application. After that add spring-boot-starter-thymeleaf dependency and java version.

<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">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>codesjava</groupId>
  <artifactId>SpringBoot04</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
 
  <name>SpringBoot04</name>
  <url>http://maven.apache.org</url>
 
   <properties>
    <java.version>1.8</java.version>  
  </properties>
 
  <parent>  
    <groupId>org.springframework.boot</groupId>  
    <artifactId>spring-boot-starter-parent</artifactId>  
    <version>1.4.2.RELEASE</version>  
 </parent>  
 
  <dependencies>
     <dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>
 
	<!-- hot swapping, disable cache for template, enable live reload -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>	
 
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
 
  <build>
		<plugins>
			<!-- Package as an executable jar or war -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

Now, right click on project -> click on Maven -> click on update project -> Popup window will open -> click Ok to update the project.
It is a initializer class which runs a SpringApplication.
App.java

package com.w3schools;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
 
@SpringBootApplication  
public class App extends SpringBootServletInitializer {
	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(App.class);
	}
    public static void main( String[] args ) {
    	SpringApplication.run(App.class, args);  
    }
}

HelloWorldController.java

package w3schools;
 
import java.util.Map;
 
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HelloWorldController {
	private String message = "Hello World";
 
	@RequestMapping("/")
	public String welcome(Map&lt;String, Object&gt; model) {
		model.put("message", this.message);
		return "helloWorld";
	}
}

helloWorld.jsp

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Insert title here</title>
</head>
<body>
<h2>Spring Boot Web Thymeleaf Example</h2>
<h2>
	<span th:text="'Message: ' + ${message}"></span>
</h2>
</body>
</html>

Hit http://localhost:8080/ in the browser, following output will come.
Spring security