JavaWeb学习之路——SpringBoot 中thymeleaf模板用法
thymeleaf模板用法
thymeleaf通过它特定的语法,对HTML的标记做渲染,能够访问后台的动态数据,实现静态html界面的动态化
1.添加架包
<!--引入动态模板-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.创建目录
创建src/main/resource/templates包:该目录安全,该目录下内容不能够通过外界直接访问
1)controller里面加上属性
用@Controller方式返回视图,默认会绑定/resource/templates里面的html
@RequestMapping("/test")
public String index(User user, Model model) {
model.addAttribute("user",user);
System.out.println(user.toString());
return "index";
}
2)html 格式,th:text 显示值
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Test Thymeleaf</title>
</head>
<body>
<span th:text="hello"></span>
<span th:text="${user.name}"></span>
<span th:text="${user.password}"></span>
</body>
</html>
3)结果展示:
视图返回结果:
视图返回结果: