- 1.1 添加依赖
<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>
- 1.2 创建存放视图层的目录
目录位置:src/main/resources/templates
templates:该目录是安全的,一位置该目录下的内容是不允许外接直接访问的。
thymeleaf的基本使用:
- 2.1 thymeleaf的特点:
- thymeleaf是通过它特定的语法对html的标记做渲染。
- 2.2 编写ThymeleafController 用来跳转页面
@Controller
public class ThymeleafController {@RequestMapping("/showThymeleaf")public String showInfo(Model model){model.addAttribute("msg","Thymeleaf 第一个案例");return "index";}
}
- 在src/main/resources/路径下创建templates目录,并添加index.html页面文件
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf入门</title>
</head>
<body>
<span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>
</body>
</html>
- 2.3 浏览器访问:http://localhost:8080/showThymeleaf
本文源码下载:
github地址:
https://github.com/gb-heima/Spring-Boot-Actual-Combat/tree/master/parent/spring-boot-chapter-7