33- Inserting String "Hello world" from java into thymeleaf template

 

Create a String "Hello World" in java and insert into the required position  on Thymeleaf Template to generate new HTML page.

1- Creating a String in Java controller class and using model attribute

thymeleaf-springboot-tutorial -->  src/main/java --> net.javaguides.springboot --> HelloWorldContoller.java

package net.javaguides.springboot;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloWorldController {
   
 @GetMapping("/hello")
 public String hello(Model model){
     model.addAttribute("message"," Hello World ");
     return "helloworld";
   
 }
}



2- Thymeleaf template (helloworld.html)

NOTE : In order to make your html document into thymeleaf template use   xmlns:th="//www.thymeleaf.org"


thymeleaf-springboot-tutorial --> src/main/resources --> templates -->     helloworld.html           


<!DOCTYPE html>
<html xmlns:th="//www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Thymeleaf hello World message App</title>
</head>
<body>

<h1 th:text="'Thymeleaf'+${message}+'App' "></h1>


</body>
</html>



Image 1  Showing Thymeleaf Engine working Screenshots


Image 2   -     Final Result




Comments

Popular posts from this blog

INDEX OF Zeek Spring Boot