模板引擎

JSP、Velocity、Freemarker、Thymeleaf



1、引入thymeleaf;

     <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>2.1.6</dependency>
切换thymeleaf版本
<properties><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><!-- 布局功能的支持程序  thymeleaf3主程序  layout2以上版本 --><!-- thymeleaf2   layout1--><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties>


2、Thymeleaf使用

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

使用:

1、导入thymeleaf的名称空间

2、使用thymeleaf语法;



3、语法规则

1)、th:text;改变当前元素里面的文本内容;

​ th:任意html属性;来替换原生属性的值

2)、表达式?



HelloController.java

package com.dym.controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import java.util.Arrays;
import java.util.Map;@Controller
public class HelloController {@ResponseBody@RequestMapping("/hello")public  String hello(){return "Hello World";}//查出用户数据,在页面展示@RequestMapping("/success")public String success(Map<String,Object> map){map.put("hello","<h1>你好</h1>");map.put("users",Arrays.asList("zhangsan","lisi","wangwu"));return "success";}}

success.html

<!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里面的文本内容设置为 -->
<div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">这是显示欢迎信息</div>
<hr/>
<div th:text="${hello}"></div>
<div th:utext="${hello}"></div>
<hr/><!-- th:each每次遍历都会生成当前这个标签: 3个h4 -->
<h4 th:text="${user}"  th:each="user:${users}"></h4>
<hr/>
<h4><span th:each="user:${users}"> [[${user}]] </span>
</h4></body>
</html>

模板引擎——Thymeleaf相关推荐

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

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

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

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

  3. Spring Boot 最佳实践(四)模板引擎Thymeleaf集成

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

  4. JavaWeb~模板引擎Thymeleaf总结

    文章目录 模板引擎是什么 有哪些常见的模板引擎 Thymeleaf使用流程 常用标签 链接表达式@{...} 变量表达式${...} 选择变量表达*{...} 消息表达式#{...}(了解) 回顾:几 ...

  5. 模板引擎 Thymeleaf 语法

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

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

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

  7. Spring boot(五)模板引擎 Thymeleaf

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

  8. 前端模板引擎Thymeleaf快速入门

    文章目录 1. Thymeleaf特点 2. 提供数据 3. 引入启动器 4. 静态页面 5. 测试 6.模板缓存 1. Thymeleaf特点 简单说, Thymeleaf 是一个跟 Velocit ...

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

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

最新文章

  1. redis的一些操作命令
  2. markdown demo 学习
  3. IOT(Index Organized Table)
  4. 跨平台、多浏览器页面测试
  5. idea 断点下一步快捷件_Intellij IDEA 创建控制台项目,断点调试快捷方式
  6. embedding.assign(embedding_input)的解读
  7. 认真学习系列:数据结构与算法——慕课网笔记
  8. 基于生物特征密钥生成研究 ------应用于区块链领域密钥的生成办法
  9. matlab线型与颜色
  10. K8S二进制部署---单节点master
  11. 15 个有趣的 JS 和 CSS 库
  12. 贝叶斯公式的对数似然函数_最大似然法与似然函数
  13. 循环(环形)缓冲区之Boost::circular_buffer
  14. 学生选课系统功能需求
  15. Linux JKD1.8 安装及配置
  16. 漏洞复现 a.WSO2 文件上传 (CVE-2022-29464)
  17. 计算机病毒是可以自我繁殖的特殊程序,计算机基础试题及答案
  18. vivado时序违例相关
  19. Android 适配暗黑模式
  20. vb中的clng函数

热门文章

  1. 如何更改github工程的语言属性
  2. Codeforces Round#433 简要题解
  3. 清除DataTable中的空行记录
  4. 不用加减乘除符号计算两数之和
  5. adbunknown Host service问题修复
  6. EChart.js 简单入门
  7. auto drop ssh failed ip address
  8. wxWidgets第九课 wx绘图工具
  9. mybatis_基础篇
  10. php的filesystem基本函数的学习(1)