11、模板引擎
前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

jsp支持非常强大的功能,包括能写Java代码,但是呢,我们现在的这种情况,SpringBoot这个项目首先是以jar的方式,不是war,像第二,我们用的还是嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。

那不支持jsp,如果我们直接用纯静态页面的方式,那给我们开发会带来非常大的麻烦,那怎么办呢?

SpringBoot推荐你可以来使用模板引擎:

模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,模板引擎有非常多,但再多的模板引擎,他们的思想都是一样的,什么样一个思想呢我们来看一下这张图:

模板引擎的作用就是我们来写一个页面模板,比如有些值呢,是动态的,我们写一些表达式。而这些值,从哪来呢,就是我们在后台封装一些数据。然后把这个模板和这个数据交给我们模板引擎,模板引擎按照我们这个数据帮你把这表达式解析、填充到我们指定的位置,然后把这个数据最终生成一个我们想要的内容给我们写出去,这就是我们这个模板引擎,不管是jsp还是其他模板引擎,都是这个思想。只不过呢,就是说不同模板引擎之间,他们可能这个语法有点不一样。其他的我就不介绍了,我主要来介绍一下SpringBoot给我们推荐的Thymeleaf模板引擎,这模板引擎呢,是一个高级语言的模板引擎,他的这个语法更简单。而且呢,功能更强大。

我们呢,就来看一下这个模板引擎,那既然要看这个模板引擎。首先,我们来看SpringBoot里边怎么用。

11.1、引入Thymeleaf
怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。给大家三个网址:

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

找到对应的pom依赖:可以适当点进源码看下本来的包!

<!--thymeleaf-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Maven会自动下载jar包,我们可以去看下下载的东西;

11.2、Thymeleaf分析
前面呢,我们已经引入了Thymeleaf,那这个要怎么使用呢?

我们首先得按照SpringBoot的自动配置原理看一下我们这个Thymeleaf的自动配置规则,在按照那个规则,我们进行使用。

我们去找一下Thymeleaf的自动配置类:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING;public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;private boolean checkTemplateLocation = true;private String prefix = "classpath:/templates/";private String suffix = ".html";private String mode = "HTML";private Charset encoding;
}

我们可以在其中看到默认的前缀和后缀!

我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!
11.3、测试
1、编写一个TestController

@Controller
public class TestController {@RequestMapping("/test")public String testcontroller(){return "test";}
}

@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法,并检测该方法是否使用了@RequestMapping 注解

@RequestMapping 来映射URL 到控制器类,或者是到Controller 控制器的处理方法上。当@RequestMapping 标记在Controller 类上的时候,里面使用@RequestMapping 标记的方法的请求地址都是相对于类上的@RequestMapping 而言的;当Controller 类上没有标记@RequestMapping 注解时,方法上的@RequestMapping 都是绝对路径。这种绝对路径和相对路径所组合成的最终路径都是相对于根路径“/ ”而言的。

2、编写一个测试页面 test.html 放在 templates 目录下

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>我是test</title>
</head>
<body>
<h1>Test!</h1>
</body>
</html>

3、启动项目请求测试

小结:

只要需要使用thymeleaf,只需要导入对应的依赖就可以了,我们需要将html页面放在我们的templates目录下即可

11.4、Thymeleaf 语法学习
要学习语法,还是参考官网文档最为准确,我们找到对应的版本看一下;

Thymeleaf 官网:https://www.thymeleaf.org/ , 简单看一下官网!我们去下载Thymeleaf的官方文档!

要使用thymeleaf,需要在html文件中导入命名空间的约束

xmlns:th="http://www.thymeleaf.org"

测试

1、修改测试请求,增加数据传输;

@Controller
public class TestController {@RequestMapping("/test")public String testcontroller(Model model){model.addAttribute("msg","hello springboot");return "test";}
}

补充:addAttribute往前台传数据,可以传对象,可以传List,通过el表达式 ${}可以获取到

语法
addAttribute(name,value,ns);
name
必需。
规定要添加的属性的名称。
value
可选。
规定属性的值。
ns
可选。
规定属性的命名空间。

2、编写下前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>我是test</title>
</head>
<body>
<h1>Test!</h1><!--注意:  所有的html元素都可以被thymeleaf替换接管;   th: 元素名-->
<!--th:text 就是将div中的内容设置为它指定的值,和之前学习的Vue一样--><div th:text="${msg}"></div>
</body>
</html>

3、测试

来认真研习一下Thymeleaf的使用语法!

1、我们可以使用任意的 th:attr 来替换Html中原生属性的值!


2、我们能写哪些表达式呢?

Simple expressions:(表达式语法)
Variable Expressions: ${...}:获取变量值;OGNL;1)、获取对象的属性、调用方法2)、使用内置的基本对象:#18#ctx : the context object.#vars: the context variables.#locale : the context locale.#request : (only in Web Contexts) the HttpServletRequest object.#response : (only in Web Contexts) the HttpServletResponse object.#session : (only in Web Contexts) the HttpSession object.#servletContext : (only in Web Contexts) the ServletContext object.3)、内置的一些工具对象:#execInfo : information about the template being processed.#uris : methods for escaping parts of URLs/URIs#conversions : methods for executing the configured conversion service (if any).#dates : methods for java.util.Date objects: formatting, component extraction, etc.#calendars : analogous to #dates , but for java.util.Calendar objects.#numbers : methods for formatting numeric objects.#strings : methods for String objects: contains, startsWith, prepending/appending, etc.#objects : methods for objects in general.#bools : methods for boolean evaluation.#arrays : methods for arrays.#lists : methods for lists.#sets : methods for sets.#maps : methods for maps.#aggregates : methods for creating aggregates on arrays or collections.
==================================================================================Selection Variable Expressions: *{...}:选择表达式:和${}在功能上是一样;Message Expressions: #{...}:获取国际化内容Link URL Expressions: @{...}:定义URL;Fragment Expressions: ~{...}:片段引用表达式Literals(字面量)Text literals: 'one text' , 'Another one!' ,…Number literals: 0 , 34 , 3.0 , 12.3 ,…Boolean literals: true , falseNull literal: nullLiteral tokens: one , sometext , main ,…Text operations:(文本操作)String concatenation: +Literal substitutions: |The name is ${name}|Arithmetic operations:(数学运算)Binary operators: + , - , * , / , %Minus sign (unary operator): -Boolean operations:(布尔运算)Binary operators: and , orBoolean negation (unary operator): ! , notComparisons and equality:(比较运算)Comparators: > , < , >= , <= ( gt , lt , ge , le )Equality operators: == , != ( eq , ne )Conditional operators:条件运算(三元运算符)If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)Default: (value) ?: (defaultvalue)Special tokens:No-Operation: _

测试:

1、编写一个Controller

@Controller
public class Test2Controller {@RequestMapping("/test2")public String test2(Map<String,Object> map){map.put("msg1","<h1>Hello</h1>");map.put("users", Arrays.asList("李泌","张小敬"));return "test2";}
}

2、测试页面取出数据

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>我是test2</title>
</head>
<body>
<h1>我是test2页面</h1><!--不转义-->
<div th:text="${msg1}"></div>
<!--转义-->
<div th:utext="${msg1}"></div><!--遍历数据-->
<!--th:each每次遍历都会生成当前这个标签:官网#9-->
<h4 th:each="user:${users}" th:text="${user}"></h4><h4><!--行内写法:官网#12--><span th:each="user:${users}">[[${user}]]</span>
</h4></body>
</html>

3、测试

springboot11 模板引擎相关推荐

  1. SpringBoot (三) :SpringBoot使用Freemarker模板引擎渲染web视图

    什么是Freemarker FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页.电子邮件.配置文件.源代码等)的通用工具. 它不是面向最终用户的,而 ...

  2. SpringBoot-web开发(三): 模板引擎Thymeleaf

    [SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) 目录 1. 引入 ...

  3. php codeigniter 语言,php – codeigniter模板引擎,包括语言解析器

    不幸的是,CI内置的模板解析器类没有此功能.你可以在 sparks directory中环顾四周,有多个火花集成了许多模板引擎,如smarty或twig,可以通过调整来创建这样的东西. 此外,您可以尝 ...

  4. php smarty模板引擎 性能,smarty性能低?直接使用php模板引擎吧

    skymvc框架使用的php模板引擎 1.[代码][PHP]代码 class smarty{ public $template_dir = '';//模版文件夹 public $cache_dir = ...

  5. java freemarker 模版_Java模板引擎-FreeMarker

    简介: FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写.FreeMarker我们的第一印象是用来替代JSP的,但是与JSP不同的是FreeMarker模板可 ...

  6. 模板引擎:VelocityFreeMarker(转)

    Velocity,名称字面翻译为:速度.速率.迅速,用在Web开发里,用过的人可能不多,大都基本知道和在使用Struts,到底Velocity和Struts(Taglib和Tiles)是如何联系?在技 ...

  7. 【JavsScript】推荐五款流行的JavaScript模板引擎

    摘要:Javascript模板引擎作为数据与界面分离工作中最重要一环,受到开发者广泛关注.本文通过开发实例解析五款流行模板引擎:Mustache.Underscore Templates.Embedd ...

  8. php 简单模板引擎,PHP 实现简单的模板引擎

    模板引擎作为视图层和模型曾分离的一种解决方案. 首先我们新建一个Template.class.php 的文件 '.m', //设置模板文件'templateDir' => 'template/' ...

  9. php模板引擎如何实现,php模板引擎技术简单实现

    用了smarty,tp过后,也想了解了解其模板技术是怎么实现,于是写一个简单的模板类,大致就是读取模板文件->替换模板文件的内容->保存或者静态化 tpl.class.php主要解析 as ...

最新文章

  1. centos6.10中部署percona-mysql双实例的方法
  2. tortoisesvn › prefer local prefer repository
  3. .NET Core开发实战(第21课:中间件:掌控请求处理过程的关键)--学习笔记(上)...
  4. redis 计数器 java_Redis 的 8 大应用场景!
  5. 积累命令、用户、正则表达式
  6. 华为效仿苹果卖高价手机?滴滴顺风车开放灰度测试;苹果官微被投诉“攻陷”| 极客头条...
  7. 一次新业务接入联调及上线支持的出差经验小结
  8. Zotero——论文管理神器
  9. MySQL 8.0.28 忘记密码,重置密码
  10. Mac电脑使用:终端的管理员用户和普通用户的自由切换方式
  11. 门窗计算机公式,窗户的计算公式是什么
  12. pwnable.kr第二遍---mistake
  13. 海瑞菌的web前端学习直播间
  14. C++STL之string类
  15. 代码可读性为什么重要啊....人家读不出来不是更安全吗?
  16. vivo X系列为什么能不断拉高手机上限?
  17. biblatex中参考文献期刊名缩写的实现
  18. 如何将CAD图块转换成外部参照?
  19. js中match函数的用法
  20. sun公司:太阳的升起与衰落

热门文章

  1. 【Qtcreator】qtcreator的基本使用方法
  2. Bootstrap 模态框(Modal)
  3. uniapp页面跳转出现白屏(APP与小程序)
  4. vue cli3 项目中解决跨域
  5. linux程序文本,Linux之文本处理
  6. linux服务器数据备份到本地硬盘_等保数据备份和恢复关键点,这些你该知道!...
  7. 虚幻四中怎么保持导入模型坐标_[CG分享]|虚幻引擎5 技术解析
  8. python 短视频_短视频篇 | Python 带你进行短视频二次创作
  9. 《代码的未来》读书笔记:内存管理与GC那点事儿
  10. Day06,selenium的剩余用法、万能登录破解和爬取京东商品信息,及破解极验滑动验证码...