Thymeleaf是新一代Java模板引擎,类似于Velocity、FreeMarker等传统Java模板引擎。与传统Java模板引擎不同的是,Thymeleaf支持HTML原型,既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果。同时,Spring Boot提供了Thymeleaf自动化配置解决方案,因此在Spring Boot中使用Thymeleaf非常方便。
Spring Boot整合Thymeleaf主要可通过如下步骤:
1. 创建工程,添加依赖新建一个Spring Boot工程,然后添加spring-boot-starter-web和spring-boot-starter-thymeleaf依赖,代码如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
ThymeleafSpring Boot为Thymeleaf提供了自动化配置类ThymeleafAutoConfiguration,相关的配置属性在ThymeleafProperties类中,由此配置可以看到,默认的模板位置在classpath:/templates/,默认的模板后缀为.html。如果使用IntelliJ IDEA工具创建Spring Boot项目,templates文件夹默认就会创建。
当然,如果开发者想对默认的Thymeleaf配置参数进行自定义配置,那么可以直接在application.properties中进行配置,部分常见配置如下:
#是否开启缓存,默认true
spring.thymeleaf.cache=true
#检查模板是否存在,默认true
spring.thymeleaf.check-template=true
#检查模板位置是否存在,默认true
spring.thymeleaf.check-template-location=true
#文件编码
spring.thymeleaf.encoding=UTF-8
#模板文件位置
spring.thymeleaf.prefix=classpath:/templates/
#Content-type配置
spring.thymeleaf.servlet.content-type=text/html
#模板文件后缀
spring.thymeleaf.suffix=.html
server.port=8099
配置控制器
创建Book实体类,然后在Controller中返回ModelAndView,代码如下:
Book.java
package com.shrimpking.pojo;
/**
* @author user1
*/
public class Book
{
private int id;
private String name;
private String author;
public Book()
{
}
public Book(int id, String name, String author)
{
this.id = id;
this.name = name;
this.author = author;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
@Override
public String toString()
{
return "Book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + '}';
}
}
BookController.java
package com.shrimpking.controller;
import com.shrimpking.pojo.Book;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by IntelliJ IDEA.
*
* @Author : Shrimpking
* @create 2023/6/3 11:37
*/
@Controller
public class BookController
{
@GetMapping("/books")
public ModelAndView books()
{
//图书列表
List<Book> books = new ArrayList<Book>();
//创建图书
Book book1 = new Book(1,"一个王者","橘白");
//创建图书,并赋值
Book book2 = new Book();
book2.setId(2);
book2.setName("两个棋子");
book2.setAuthor("未知");
books.add(book1);
books.add(book2);
//创建回传视图对象
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("books",books);
//设定视图名,也就是前端页面名称
modelAndView.setViewName("books");
return modelAndView;
}
}
创建视图
在resources目录下的templates目录中创建books.html,具体代码如下:
books.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1">
<tr>
<th>图书编号</th>
<th>图书名称</th>
<th>图书作者</th>
</tr>
<tr>
<tr th:each="book:${books}">
<td th:text="${book.id}"></td>
<td th:text="${book.name}"></td>
<td th:text="${book.author}"></td>
</tr>
</table>
</body>
</html>
运行截图