模板引擎——Thymeleaf
模板引擎
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相关推荐
- Spring Boot (四)模板引擎Thymeleaf集成
一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...
- 六十四、SpringBoot中的模板引擎Thymeleaf
@Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 使用Sprin ...
- Spring Boot 最佳实践(四)模板引擎Thymeleaf集成
## 一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供X ...
- JavaWeb~模板引擎Thymeleaf总结
文章目录 模板引擎是什么 有哪些常见的模板引擎 Thymeleaf使用流程 常用标签 链接表达式@{...} 变量表达式${...} 选择变量表达*{...} 消息表达式#{...}(了解) 回顾:几 ...
- 模板引擎 Thymeleaf 语法
模板引擎 Thymeleaf 1. Thymeleaf 简介 Thymeleaf[taɪm lif],百里香叶,是一个流行的模板引擎,该模板引擎采用 Java 语言开发.Java 中常见的模板引擎有 ...
- SpringBoot-web开发(三): 模板引擎Thymeleaf
[SpringBoot-web系列]前文: SpringBoot-web开发(一): 静态资源的导入(源码分析) SpringBoot-web开发(二): 页面和图标定制(源码分析) 目录 1. 引入 ...
- Spring boot(五)模板引擎 Thymeleaf
Thymeleaf 是一款用于渲染 XML/XHTML/HTML 5 内容的模板引擎.类似 JSP.Velocity.FreeMaker 等,它也可以轻易的与 Spring MVC 等 Web 框架进 ...
- 前端模板引擎Thymeleaf快速入门
文章目录 1. Thymeleaf特点 2. 提供数据 3. 引入启动器 4. 静态页面 5. 测试 6.模板缓存 1. Thymeleaf特点 简单说, Thymeleaf 是一个跟 Velocit ...
- SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)
Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...
最新文章
- redis的一些操作命令
- markdown demo 学习
- IOT(Index Organized Table)
- 跨平台、多浏览器页面测试
- idea 断点下一步快捷件_Intellij IDEA 创建控制台项目,断点调试快捷方式
- embedding.assign(embedding_input)的解读
- 认真学习系列:数据结构与算法——慕课网笔记
- 基于生物特征密钥生成研究 ------应用于区块链领域密钥的生成办法
- matlab线型与颜色
- K8S二进制部署---单节点master
- 15 个有趣的 JS 和 CSS 库
- 贝叶斯公式的对数似然函数_最大似然法与似然函数
- 循环(环形)缓冲区之Boost::circular_buffer
- 学生选课系统功能需求
- Linux JKD1.8 安装及配置
- 漏洞复现 a.WSO2 文件上传 (CVE-2022-29464)
- 计算机病毒是可以自我繁殖的特殊程序,计算机基础试题及答案
- vivado时序违例相关
- Android 适配暗黑模式
- vb中的clng函数