Java|Java SpringBoot模板引擎之 Thymeleaf入门详解
目录
- 模板引擎简介
- 引入Thymeleaf模板引擎
- 分析Thymeleaf模板引擎
- 测试Thymeleaf模板引擎
- 1、编写一个TestController
- 2、编写一个测试页面 test.html 放在 templates 目录下
- 3、启动项目请求测试
- 4、结论
- Thymeleaf入门:
- 1、修改测试请求,增加数据传输
- 2、使用thymeleaf
- 3、我们去编写下前端页面
- 4、启动测试!
- thymeleaf语法学习
- 1、使用任意的 th:attr 来替换Html中原生属性的值!
- 2、表达式语法:
- 2、测试页面取出数据
- 3、启动项目测试!
- 总结:
模板引擎简介 如果我们直接用纯静态页面方式,必然会给开发带来很大麻烦,所以springboot推荐使用模板引擎,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf!模板引擎的本质思想如下图:
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/77c0874507254ab1a44042b73cd4e48f.jpg)
文章图片
引入Thymeleaf模板引擎 Thymeleaf 官网:Thymeleaf
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/2a9c8ce1f82a408f951d26fc053cf3ea.jpg)
文章图片
Spring官方文档:
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
org.springframework.boot spring-boot-starter-thymeleaf
Maven自动下载jar包,下图试maven下载的东西;
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/bcb76d7aefb54b50b083b6e7b383db8e.jpg)
文章图片
分析Thymeleaf模板引擎 首先按照SpringBoot的自动配置原理来看一下我们这个Thymeleaf的自动配置规则,再按照这个规则,我们进行使用。可以先去看看Thymeleaf的自动配置类:ThymeleafProperties
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/cb28499ff65543b8a5ab16d119dbb6d7.jpg)
文章图片
我们可以在配置文件看到默认的前缀和后缀!
我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染。
测试Thymeleaf模板引擎
1、编写一个TestController
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/15a1caaac78143e2abfeaa0aeca8c238.jpg)
文章图片
2、编写一个测试页面 test.html 放在 templates 目录下
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/923567bcc3fb49938872925beeab60ff.jpg)
文章图片
3、启动项目请求测试
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/9a69a437363449b7b59c109b48c65d7e.png)
文章图片
4、结论
只要需要使用thymeleaf,只需要导入对应的依赖就可以了,然后将html放在templates的目录下即可
Thymeleaf入门: 我们可以查看下Thymeleaf 官网:https://www.thymeleaf.org/
简单练习:查出一些数据,在页面中展示
1、修改测试请求,增加数据传输
@Controllerpublic class TestController { @RequestMapping("/t1")public String test1(Model model){//存入数据model.addAttribute("msg","Hello,Thymeleaf"); //classpath:/templates/test.htmlreturn "test"; }}
2、使用thymeleaf
我们要使用thymeleaf,需要在html文件中导入命名空间的约束,方便提示。
xmlns:th=http://www.thymeleaf.org
3、我们去编写下前端页面
九阳真经---龍弟 - 锐客网 测试页面
4、启动测试!
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/1c361854e79149b49eae3902b1f24fea.png)
文章图片
thymeleaf语法学习
1、使用任意的 th:attr 来替换Html中原生属性的值!
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/0e00b0bc4716417c8068a5bfd5e860f2.png)
文章图片
2、表达式语法:
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/d76bda8be7bd4f4db202b1785efc8b0e.png)
文章图片
练习测试
@Controllerpublic class TestController {@RequestMapping("/t2")public String test2(Map map){//存入数据map.put("msg","Hello,SpringBoot"); map.put("users", Arrays.asList("dragon","longdi")); //classpath:/templates/test.htmlreturn "test"; }}
2、测试页面取出数据
九阳真经---龍弟 - 锐客网 测试页面 [[${user}]]
3、启动项目测试!
![Java|Java SpringBoot模板引擎之 Thymeleaf入门详解](https://img.it610.com/image/info11/2690b52b0c034152be50f2c667735cff.png)
文章图片
总结: 由于thymeleaf很多语法样式,我们现在学了也会忘记,因此,在学习过程中,需要使用什么,根据官方文档来查询,所以要熟练使用官方文档!
【Java|Java SpringBoot模板引擎之 Thymeleaf入门详解】到此这篇关于Java SpringBoot模板引擎之 Thymeleaf入门详解的文章就介绍到这了,更多相关Java SpringBoot Thymeleaf内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Activiti(一)SpringBoot2集成Activiti6
- 事件代理
- SpringBoot调用公共模块的自定义注解失效的解决
- opencv|opencv C++模板匹配的简单实现
- Java|Java OpenCV图像处理之SIFT角点检测详解
- 解决SpringBoot引用别的模块无法注入的问题
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题