简介
Thymeleaf是面向Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。,可以完全替代jsp,也是spring boot官方推荐的模版引擎
Thymeleaf优势
1.可以独立运行 前后端分离的时候 前端可以直接运行模版进行样式调整
2.不破坏html整体结构,更贴向html
3.可以使用模版实现JSTL、 OGNL表达式效果
Thymeleaf简单使用
1.引入pom依赖
<!--thymeleaf模版--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.新建一个Contorller
@Controller public class HelloWordContorller { @RequestMapping("/helloword") public String helloWord(Model model){ List<StudentConfig> studentConfigList=new ArrayList<StudentConfig>(); for(int i=0;i<10;i++){ studentConfigList.add(new StudentConfig("小明"+i,i)); } model.addAttribute("list",studentConfigList); return "index"; } }
3.再resource/template下面新建一个html页面
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> table td{ text-align: center; } </style> </head> <body> <table style="width:100%"> <thead> <tr> <th>学生姓名</th> <th>学生年龄</th> </tr> </thead> <tbody> <tr th:each="item : ${list}"> <td th:text="${item.name}">小明</td> <td th:text="${item.age}">20</td> </tr> </tbody> </table> </body> </html>
xmlns:th="http://www.thymeleaf.org" 将静态页面转换为动态的视图,需要进行动态处理的元素将使用“th:”前缀。
访问输出
thymeleaf模版引擎跟jsp比起来是否更易读。如果写jsp的话 里面有很多自定义标签 前端人员根本也无法阅读
还有就是模版是html是可以直接运行的
4.找到模版地址
打开
所以前端是可以直接根据模版进行样式调整