Spring Boot Thymeleaf View

Spring Boot Thymeleaf View with features, project, starter project wizard, cli, application, annotations, dm, properties, actuator, thymeleaf view, jpa, jdbc

Spring Boot Thymeleaf View

Spring Boot Thymeleaf View

Thymeleaf

It is a server side Java template engine for web application. It's main goal is to bring elegant natural templates to your web application.

It can be integrate with Spring Framework and ideal for HTML5 Java web applications.

In the following example, we are using Thymeleaf as our HTML template and rendering it from controller.

In order to use Thymeleaf we must add it into our pom.xml file like:

  1. <dependency>  
  2.         <groupId>org.springframework.boot</groupId>  
  3.         <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  4. </dependency>  

after that mentioning it in our template that we are using this library. Like:

  1. <html xmlns:th="https://thymeleaf.org">  

Our project has the following directly structure.

In our project, we have following files.

// index.html

  1. <html lang="en">  
  2.     <head>  
  3.         <title>Index Page</title>  
  4.     </head>  
  5.     <body>  
  6.         <form action="save" method="post">  
  7.             <table>  
  8.                 <tr>  
  9.                     <td><label for="user-name">User Name</label></td>  
  10.                     <td><input type="text" name="name"></input></td>  
  11.                 </tr>  
  12.                 <tr>  
  13.                     <td><label for="email">Email</label></td>  
  14.                     <td><input type="text" name="email"></input></td>  
  15.                 </tr>  
  16.                 <tr>  
  17.                     <td></td>  
  18.                     <td><input type="submit" value="Submit"></input></td>  
  19.                 </tr>  
  20.             </table>  
  21.         </form>  
  22.     </body>  
  23. </html>  

// user-data.html

 
  1. <html xmlns:th="https://thymeleaf.org">  
  2.     <table>  
  3.         <tr>  
  4.             <td><h4>User Name: </h4></td>  
  5.             <td><h4 th:text="${user.name}"></h4></td>  
  6.         </tr>  
  7.         <tr>  
  8.             <td><h4>Email ID: </h4></td>  
  9.             <td><h4 th:text="${user.email}"></h4></td>  
  10.         </tr>  
  11.     </table>  
  12. </html>  

// User.java

  1. package com.tpoint;  
  2. public class User {  
  3.     String name;  
  4.     String email;  
  5.     public String getName() {  
  6.         returnname;  
  7.     }  
  8.     public void setName(String name) {  
  9.         this.name = name;  
  10.     }  
  11.     public String getEmail() {  
  12.         returnemail;  
  13.     }  
  14.     public void setEmail(String email) {  
  15.         this.email = email;  
  16.     }  
  17.   
  18. }  

// pom.xml

  1. <project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">  
  2. <modelVersion>4.0.0</modelVersion>  
  3. <groupId>com.javatpoint</groupId>  
  4. <artifactId>spring-boot-example</artifactId>  
  5. <version>0.0.1-SNAPSHOT</version>  
  6. <name>JavaTpoint Spring Boot Example</name>  
  7. <parent>  
  8.     <groupId>org.springframework.boot</groupId>  
  9.     <artifactId>spring-boot-starter-parent</artifactId>  
  10.     <version>1.4.2.RELEASE</version>  
  11. </parent>  
  12. <dependencies>  
  13.     <dependency>  
  14.         <groupId>org.springframework.boot</groupId>  
  15.         <artifactId>spring-boot-starter-web</artifactId>  
  16.     </dependency>  
  17.     <dependency>  
  18.         <groupId>org.springframework.boot</groupId>  
  19.         <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  20.     </dependency>  
  21.     <dependency>  
  22.         <groupId>org.springframework.boot</groupId>  
  23.         <artifactId>spring-boot-starter-data-solr</artifactId>  
  24.     </dependency>  
  25. </dependencies>  
  26. <properties>  
  27.     <java.version>1.8</java.version>  
  28. </properties>  
  29. </project>  

// HomeController.java

  1. package com.point;  
  2. import org.springframework.web.bind.annotation.ModelAttribute;  
  3. import org.springframework.web.bind.annotation.RequestMapping;  
  4. import org.springframework.web.bind.annotation.RequestMethod;  
  5. import org.springframework.web.servlet.ModelAndView;  
  6. import org.springframework.stereotype.Controller;  
  7. @Controller  
  8. public class HomeController {  
  9.     @RequestMapping("/")  
  10.     public String index(){  
  11.         return"index";  
  12.     }  
  13.     @RequestMapping(value="/save", method=RequestMethod.POST)  
  14.     public ModelAndView save(@ModelAttribute User user){  
  15.         ModelAndView modelAndView = new ModelAndView();  
  16. modelAndView.setViewName("user-data");      
  17. modelAndView.addObject("user", user);    
  18. returnmodelAndView;  
  19.     }  
  20. }  

// SpringBootExampleApplication.java

  1. package com.tpoint;  
  2. import org.springframework.boot.SpringApplication;  
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  4. @SpringBootApplication  
  5. public class SpringBootExample {  
  6.     public static void main(String[] args) {  
  7.         SpringApplication.run(SpringBootExample.class, args);  
  8.     }  
  9. }  

Output:

After running the application. It produce the following output.

Index Page

Entering details

After submitting form