1.模板引擎原理

JSP、Velocity、Freemarker、Thymeleaf 都是模板引擎。SpringBoot推荐的Thymeleaf;语法更简单,功能更强大;

Thymeleaf模板引擎

  Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎。类似JSP, Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成 作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够 直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用
  Spring Boot推荐使用Thymeleaf、Freemarker等后现代的模板引擎技术;一但导入相 关依赖,会自动配置ThymeleafAutoConfiguration、FreeMarkerAutoConfiguration。

2.引入依赖

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

@Controller
@RequestMapping("/hello")
public class HelloWorldController
{
@RequestMapping("/success")
public String success() {
return "success";
}
}

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

但是每次改动html代码都需要重新启动项目,太不方便

Thyme leaf官方文档:https://www.thymeleaf.org/

<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">

  <head>
<title>Good Thymes Virtual Grocery</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="all"
href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
</head> <body> <p th:text="#{home.welcome}">Welcome to our grocery store!</p> </body> </html>

  

IDEA会有提示,但是eclipse没有,eclipse需要安装插件才能提示

插件地址为: http://www.thymeleaf.org/eclipse-plugin-update-site/

最后:右键项目 >> Thymeleaf >> Add Thymeleaf Nature.

使用:

1、导入thymeleaf的名称空间

<html lang="en" xmlns:th="http://www.thymeleaf.org">

2.类中设置变量

@RequestMapping("/success")
public String success(Map<String,Object> map) {
map.put("result","hello world");
return "success";
}

3.页面取值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
  <head>
  <meta charset="UTF-8">
  <title>Title</title>
  </head>
  <body>
<h1>成功!</h1>
<!--th:text 将div里面的文本内容设置为${hello}变量的值,覆盖 文本内容:“这是显示欢迎信息”-->
   <div th:text="${result}">这是显示欢迎信息</div>
  </body>
</html>

Thymeleaf官方文档:属性优先级

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功</title>
</head>
<body>
<h1><font color="red"> 成功 sdfsfas</font></h1>
<div th:text="${result}"></div>
<div th:utext="${result}"></div> <hr> <!-- 生成4个h4标签-->
<h4 th:text="${user}" th:each="user:${users}"></h4> <hr>
<h4>
<!-- 生产3个span标签-->
<span th:each="user:${users}">[[${user}]]!</span>
</h4>
</body>
</html>
    @RequestMapping("/success")
public String success(Map<String,Object> map) {
map.put("result","<h1>hello world</h1>");
map.put("users", Arrays.asList("xiao","chao","yi"));
return "success";
}


IDEA 会有提示

8.SpringBoot 模板引擎 Thymeleaf的更多相关文章

  1. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器

    目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...

  2. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  3. 新一代Java模板引擎Thymeleaf

    新一代Java模板引擎Thymeleaf-spring,thymeleaf,springboot,java 相关文章-天码营 https://www.tianmaying.com/tutorial/u ...

  4. SpringBoot系列:Spring Boot使用模板引擎Thymeleaf

    一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在jav ...

  5. SpringBoot入门系列(四)整合模板引擎Thymeleaf

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  6. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)

    Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...

  7. springboot:Java模板引擎Thymeleaf介绍

    Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎.类似JSP,Velocity,FreeMaker等,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用 ...

  8. 模板引擎Thymeleaf

    1.Thymeleaf简介 Thymeleaf 是一个跟 Velocity.FreeMarker 类似的模板引擎,它可以完全替代 JSP .相较与其他的模板引擎,它有如下三个极吸引人的特点 Thyme ...

  9. Spring Boot中使用模板引擎Thymeleaf

    一.Thymeleaf简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用Java语言开发.Java中常见的模板引擎有Velocity.Freemaker.Th ...

随机推荐

  1. ionic动态切换主题皮肤

    本来想通过css自定义属性值: :root{ --red:red; } p{ color:var(--red); } div{ background:var(--red); } 在ionic2设置动态 ...

  2. panda迭代

    1.注意 - 不要尝试在迭代时修改任何对象.迭代是用于读取,迭代器返回原始对象(视图)的副本,因此更改将不会反映在原始对象上. 2.itertuples()方法将为DataFrame中的每一行返回一个 ...

  3. apache自带压力测试工具ab的使用及解析

    当你搭建了apache服务器并在上面部署了web网站,在网站运行前,为了使apache服务器的性能得到更好的应用,我们可以先对其进行压力测试.进行压力测试其实非常简单,我们也不用再额外下载安装什么测试 ...

  4. ultraEdit MAC 破解方法

    安装了个UltraEdit 但是需要验证码,太麻烦了,破解方法: 拷贝附件(command +c )然后在MAC的底下点击访达==>应用程序==>UltraEdit==>右击 显示包 ...

  5. golang自定义struct字段标签

    原文链接: https://sosedoff.com/2016/07/16/golang-struct-tags.html struct是golang中最常使用的变量类型之一,几乎每个地方都有使用,从 ...

  6. BZOJ1115[POI2009]石子游戏——阶梯Nim游戏

    题目描述 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子,但是要保证操作后仍然满足初始时的条件谁没有石子可移时输掉游戏.问先手是否必 ...

  7. LightOJ - 1341 Aladdin and the Flying Carpet (算术基本定理)

    题意: 就是....求a的所有大于b的因子有多少对 算术基本定理求 所有因子 阿欧...偷张图. 注意范围 就好  ..... 解析: 在1 -1012的范围内求大于b的所有a的因子的对数(有几对) ...

  8. day14 装饰器模拟验证附加功能

    user_list=[ {'}, {'}, {'}, {'}, ] current_dic={'username':None,'login':False} def auth_func(func): d ...

  9. 用树莓派改装电风扇及实现Android遥控

    最近天气很热,我租的房子又没有空调,基本上风扇一开就是一晚上,结果经常起床后发现口干舌燥的.我觉得这肯定是因为整晚吹风扇搞的,不管是不是,反正我觉得就是了.不开风扇吧,热!开风扇吧,早上起来不舒服,怎 ...

  10. [hgoi#2019/2/24]玄学考试

    感想 对于这次考试,真的不想说什么了,太玄学了!!! t1输出比标准输出长,这是什么操作???难道要关文件???但是交到oj上又A掉了.这是什么操作. t2还好,没有出什么意外...但是要吐槽一下出题 ...