Thymeleaf简介
Thymeleaf是一个Java模板引擎,支持html、xml、text、javascript、css、raw这几种模型。
使用Thymeleaf首先需要引入命名空间
<html xmlns:th="http://www.thymeleaf.org">
SpringBoot并不推荐使用jsp,但是支持一些模板引擎技术,如:Freemarker,Thymeleaf,Mustache
Thymeleaf集成
(1)引入启动器
(2)SpringBoot会自动为Thymeleaf注册一个视图解析器:ThymeleafViewResolver
- 默认前缀:classpath:/templates/
- 默认后缀:.html
如果我们返回视图:users,会指向到 classpath:/templates/users.html;一般我们无需进行修改,默认即可。
测试.
(3)Controller提供数据
使用ModelMap将数据与页面合在一起
@RequestMapping(path="/test01",method = {
RequestMethod.GET})
public String test01(ModelMap modelMap){
//带数据建议大家使用ModelMap
//name jack
modelMap.addAttribute("name","jack");
return "person-list"