Spring boot MVC using Thymeleaf
You can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSP.
This tutorial shows how to run a simple spring boot application using Thymeleaf as a templating engine.
If you use Thymeleaf, It looks for resources by surrounding the view name with a prefix and suffix. The prefix is spring.thymeleaf.prefix, and the suffix is spring.thymeleaf.suffix. The values of the prefix and suffix default to ‘classpath:/templates/’ and ‘.html’, respectively. You can override ThymeleafViewResolver by providing a bean of the same name.
Configure starters (pom.xml)
Add web and thymeleaf dependency to maven or gradle build.gradle file, There are a lot of templating technology available and are supported by Spring MVC you can and based on your need.
<?xml version="1.0" encoding="UTF-8"?> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.candidjava</groupId> <artifactId>Spring-Boot-Web-MVC</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>Spring-Boot-Web-MVC Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <finalName>Spring-Boot-Web-MVC</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Thymeleaf configuration (application.yml)
Autoconfiguration will load all your needs, you can override it based on your need, refer Spring boot common properties for detailed configuration.
server: port: ${PORT:8081} spring: thymeleaf: cache: false check-template: true check-template-location: true content-type: text/html enabled: true encoding: UTF-8
Simple MVC Controller (StudentController.java)
Below code return, “student” as a view name and Model object contains the data to be viewed in the output page.
package com.candidjava.springboot.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; @Controller @RequestMapping(value = "/student") public class StudentController { @Autowired StudentService service; @GetMapping("/getAll") public String get(Model model) { List<Student> students = service.getAllStudents(); model.addAttribute("students", students); return "student"; } }
Thymeleaf template (student.html)
Once the Controller returns the “student” as a view name Thymeleaf will return this sudent.html as a view page.
<html xmlns:th="http://www.thymeleaf.org"> <div th:switch="${students}"> <h2 th:case="null">No students yet!</h2> <div th:case="*"> <h2>Students</h2> <table> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> </tr> </thead> <tbody> <tr th:each="student : ${students}"> <td th:text="${student.id}"></td> <td th:text="${student.name}"></td> <td th:text="${student.age}"></td> </tr> </tbody> </table> </div> </div>
Sample Service (StudentService.java)
package com.candidjava.springboot.service; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.stereotype.Service; import com.candidjava.springboot.models.Student; import com.candidjava.springboot.service.StudentService; @Service public class StudentService { private List<Student> studentList = new ArrayList<Student> (Arrays.asList( new Student("1", "ram", "20"), new Student("2", "arun", "21"), new Student("3", "karthick", "22") )); public List<Student> getAllStudents() { return studentList; } }
Sample model (Student.java)
package com.candidjava.springboot.models; public class Student { private String id; private String name; private String age; public Student(String id, String name, String age) { this.id = id; this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
Start as spring Boot application (SpringWebMVCApplication.java)
Launch this application as Spring boot and hit http://localhost:8080/getAll in browser.
package com.candidjava.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringWebMVCApplication { public static void main(String[] args) { SpringApplication.run(SpringWebMVCApplication.class, args); } }
Download source code
Download thymeleaf sample source code from my github account (Click here)