1. 什么是模板引擎

将模板文件和数据通过模板引擎生成一个HTML代码

jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf。

图来源于:https://www.jianshu.com/p/e4aa40458dfd

2. 为什么要用模板引擎

SpringBoot这个项目首先是以jar的方式,不是war,而且还是用嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

3. 常见的模板引擎

  • JSP

  • Thymeleaf

  • Velocity

  • Freemarker

    模板引擎比较:https://blog.csdn.net/az44yao/article/details/99887733

4. 引入Thymeleaf

Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf
Spring官方文档:找到我们对应的版本
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter

  1. 导入依赖
<!--模板引擎:thymeleaf-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
  1. 编写controller类
@Controller
public class HelloController {@GetMapping("/test")public String test(Model model) {model.addAttribute("msg","Hello SpringBoot");return "test";}
}
  1. 编写在resources下的templates建立test.html

注意:

  • 使用前需引入约束
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  • 所有的htmL元素都可以被thymeLeaf替换接管: th:元素名
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<!--所有的htmL元素都可以被thymeLeaf替换接管: th:元素名-->
<div th:text="${msg}"></div>
</body>
</html>
  1. 测试
    在浏览器输入:http://localhost:8080/test

5. Thymeleaf语法

5.1 th:text与th:utext

<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>

测试:

5.2 th:each

两种方式:推荐使用第一种

<div th:each="user:${users}" th:text="${user}"></div>
<div th:each="user:${users}">[[${user}]]</div>

测试:

Thymeleaf使用(参考博客):https://www.cnblogs.com/jerry126/p/11531310.html

参考内容:https://www.bilibili.com/video/BV1PE411i7CV
仅用于学习!

SpringBoot之模板引擎相关推荐

  1. SpringBoot配置模板引擎之视图解析器失效问题排查

      1.问题排查 在整合SpringBoot与Httl模板引擎的时候,博主曾遇到过这样的问题:通过浏览器访问一个Controller方法时,返回httl视图,但页面报404错误. 通过断点调试,访问u ...

  2. SpringBoot中模板引擎thymeleaf

    首先我们用SpringBoot创建一个支持thymeleaf的web项目 添加web支持 添加thymeleaf模板引擎 创建好该项目之后,在templates目录下创建一个普通的html文件,这个时 ...

  3. SpringBoot的模板引擎

    springboot是通过jar包的方式打包数据,不是war包,而且是嵌入式的tomcat,不支持jsp页面,但我们不能通过静态页面来写项目,我们的后台传过来的数据就不能c:forch,c:if等进行 ...

  4. SpringBoot 整合模板引擎 Thymeleaf 页面跳转失败的解决方案

    1. 出错现象 我们知道 Thymeleaf 模板引擎规定的文件路劲应该是在 templates 目录下面的. 但是笔者在开发的过程中,满足了上述要求但是还是没能映射成功. 我们来看看 index.h ...

  5. html 模板引擎 热部署,springboot系列四、配置模板引擎、配置热部署

    一.配置模板引擎 在之前所见到的信息显示发现都是以 Rest 风格进行显示,但是很明显在实际的开发之中,所有数据的显示最终都应该交由页面完成,但是这个页面并不是*.jsp 页面,而是普通的*.html ...

  6. 【模板引擎】Springboot整合ThymeleafThymeleaf基本语法

    Thymeleaf介绍 thymeleaf是一个XML/XHTML/HTML5模板引擎,可用于Web与非Web环境中的应用开发.它是一个开源的Java库,基于Apache License 2.0许可, ...

  7. 玩转springboot:thymeleaf模板引擎入门程序

    一.前言 常用的模板引擎有:JSP.Velocity.Freemarker.Thymeleaf 但是,Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.而且,语法更简单,功 ...

  8. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:"赛母李府",大部分中国 ...

  9. 六十四、SpringBoot中的模板引擎Thymeleaf

    @Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 使用Sprin ...

最新文章

  1. Git 最全命令总结都在这里了
  2. Windows7 64bit VS2013 Caffe train MNIST操作步骤
  3. 把PPT做漂亮点真的有用!图表美观能增加可信度!作者:还会影响论文引用和通过率...
  4. android中完全退出当前应用程序的四种方法
  5. NK.bin和NK.nb0学习
  6. springboot日志笔记
  7. 英特尔凌动处理器_曾押宝英特尔凌动CPU,华硕手机如今活得如何了?
  8. java中TreeSet集合如何实现元素的判重
  9. WPF 圆型布局(测量过程)
  10. 定时任务执行利器Timer和ScheduledThreadPoolExecutor使用
  11. 大学生心理健康管理系统
  12. java 三大特性_java的三大特性是什么?
  13. 视频 | 计算万物的理论
  14. Win密钥.Win7旗舰版
  15. 计算机基础知识新手学电脑输入法,新手学习电脑怎么使用键盘打字
  16. 【音频分析】短时傅立叶变换结果为啥是对称?每个结果对应的频率是多少?
  17. TP6集成JWT的步骤。
  18. C语言版家谱管理系统
  19. 微信QQ等主流应用上线国产系统UOS:界面曝光
  20. Windows PE开发环境

热门文章

  1. Codec Engine 概要
  2. 通过使用SPSS独立样本T检验,分析两组个案的差异(下)
  3. python 斗鱼弹幕的爬取一(selenium)
  4. 苹果应用分身_你喜欢用苹果手机拍照!不学会这4个功能,怪不得拍不出好照片...
  5. 主流大数据调度工具对比
  6. linux下使用代理加速下载方案集合
  7. 受到人工智能发展的影响的,主要包括哪些行业?
  8. FileUploadException: the request was rejected because no multipart boundary was found
  9. 经济危机会影响软件质量吗?
  10. Android商城排版,android排版布局学习