springboot笔记-thymeleaf
简介:
Thymeleaf 是⾯向 Web 和独⽴环境的现代服务器端 Java 模板引擎,能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。
Thymeleaf 的作用域在 HTML 标签内,类似标签的一个属性来使用
快速上手:
添加pom依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在 application.properties 中添加配置:
##关闭 Thymeleaf 的缓存,否则在开发过程中修改页面不会立刻生效需要重启,生产可配置为 true
spring.thymeleaf.cache=false
编写一个简单的页面
项目结构
spring-boot-thymeleaf
+-src
+- main
+- java
+- resources
+-static
+-templates
application.properties
+- test
+-pom.xml
在templates目录下创建一个hello.html页面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Hello</title>
</head>
<body>
<h1 th:text="${message}">Hello World</h1>
</body>
</html>
###所有使用 Thymeleaf 的页面必须在 HTML 标签声明 Thymeleaf:<html xmlns:th="http://www.thymeleaf.org">表明页面使用的是 Thymeleaf 语法。
新建Controller
@Controller
public class HelloController {
@RequestMapping("/")
public String index(ModelMap map) {
map.addAttribute("message", "http://www.baidu.com");
return "hello";
}
}
启动项目 在浏览器中输入网址:http://localhost:8080/,会出现下面的结果:
http://www.baidu.com
说明页面的值,已经成功的被后端传入的内容所替换。
常用语法:
赋值
th:text="${message}"
条件判断 If/Unless
th:if="${flag == 'yes'}"
th:unless="${flag != 'no'}"
th:unless 与 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。
for 循环
th:each="item,iterStat : ${items}"
iterStat 称作状态变量,属性有:
index,当前迭代对象的 index(从 0 开始计算);
count,当前迭代对象的 index(从 1 开始计算);
size,被迭代对象的大小;
current,当前迭代变量;
even/odd,布尔值,当前循环是否是偶数/奇数(从 0 开始计算);
first,布尔值,当前循环是否是第一个;
last,布尔值,当前循环是否是最后一个。
URL
Thymeleaf 对于 URL 的处理是通过语法@{...}来处理的
<a th:href="@{http://www.baidu.com/{type}(type=${type})}">link1</a>
也可以使用@{...}设置背景:
<div th:style="'background:url(' + @{${img url}} + ');'">
switch\case 选择
switch\case 多用于多条件判断的场景下,举例:
<div th:switch="${sex}">
<p th:case="'woman'">她是一个姑娘...</p>
<p th:case="'man'">这是一个爷们!</p>
<!-- *: case的默认的选项 -->
<p th:case="*">未知性别的一个家伙。</p>
</div>
内联 [ [ ] ]
在 javascript 代码块里访问 model 中的数据,则要使用内联的方法
内联文本:[ [...] ] 内联文本的表示方式,使用时,必须先用在 th:inline="text/javascript/none" 激活,th:inline 可以在父级标签内使用,甚至可以作为 body 的标签。
如果想在脚本中使用后端传递的值,则必须使用脚本内联:
th:inline="javascript"
基本对象
Thymeleaf 包含了一些基本对象,可以用于我们的视图中,这些基本对象使用 # 开头。
#ctx:上下文对象
#vars:上下文变量
#locale:区域对象
#request:(仅 Web 环境可用)HttpServletRequest 对象
#response:(仅 Web 环境可用)HttpServletResponse 对象
#session:(仅 Web 环境可用)HttpSession 对象
#servletContext:(仅 Web 环境可用)ServletContext 对象
内嵌变量
dates:java.util.Date 的功能方法类
calendars:类似 #dates,面向 java.util.Calendar
numbers:格式化数字的功能方法类
strings:字符串对象的功能类,contains、startWiths、prepending/appending 等
objects:对 objects 的功能类操作
bools:对布尔值求值的功能方法
arrays:对数组的功能类方法
lists:对 lists 的功能类方法
sets:set 的实用方法
maps:map 的实用方法
接下来总结一下 Thymeleaf 表达式。
表达式共分为以下五类:
变量表达式:${...}
选择或星号表达式:*{...}
文字国际化表达式:#{...}
URL 表达式:@{...}
片段表达式:~{...}
常用 th 标签
页面常用的 HTML 标签几乎都有 Thymeleaf 对应的 th 标签。
关键字 功能介绍 案例
th:id 替换 id <input th:id="'xxx' + ${collect.id}"/>
th:text 文本替换 <p th:text="${collect.description}">description</p>
th:utext 支持 html 的文本替换 <p th:utext="${htmlcontent}">conten</p>
th:object 替换对象 <div th:object="${session.user}">
th:value 属性赋值 <input th:value="${user.name}" />
th:with 变量赋值运算 <div th:with="isEven=${prodStat.count}%2==0"></div>
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
th:onclick 点击事件 th:onclick="'getCollect()'"
th:each 属性赋值 tr th:each="user,userStat:${users}">
th:if 判断条件 <a th:if="${userId == collect.userId}" >
th:unless 和 th:if 判断相反 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href 链接地址 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:switch 多路选择 配合 th:case 使用 <div th:switch="${user.role}">
th:case th:switch 的一个分支 <p th:case="'admin'">User is an administrator</p>
th:fragment 布局标签,定义一个代码片段,方便其他地方引用 <div th:fragment="alert">
th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div>
th:selected selected 选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"
th:src 图片类地址引入 <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
th:inline 定义 js 脚本可以使用变量 <script type="text/javascript" th:inline="javascript">
th:action 表单提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
th:remove 删除某个属性 <tr th:remove="all">
1.all:删除包含标签和所有的子节点;
2.body:不包含标记删除,但删除其所有的子节点;
3.tag:包含标记的删除,但不删除它的子节点;
4.all-but-first:删除所有包含标签的子节点,除了第一个。
5.none:什么也不做。这个值是有用的动态评估
th:attr 设置标签属性,多个属性可以用逗号分隔 比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少
Thymeleaf 配置
通过application.properties 文件灵活的配置 Thymeleaf 的各项特性,以下为 Thymeleaf 的配置和默认参数:
# THYMELEAF (ThymeleafAutoConfiguration)
#开启模板缓存(默认值:true)
spring.thymeleaf.cache=true
#检查模板是否存在,然后再呈现
spring.thymeleaf.check-template=true
#检查模板位置是否正确(默认值:true)
spring.thymeleaf.check-template-location=true
#Content-Type的值(默认值:text/html)
spring.thymeleaf.content-type=text/html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled=true
#模板编码
spring.thymeleaf.encoding=UTF-8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded-view-names=
#要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
spring.thymeleaf.mode=HTML5
#在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
spring.thymeleaf.prefix=classpath:/templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix=.html
#Thymeleaf 模板解析器在解析器链中的顺序,默认情况下,它排第一位,顺序从1开始,只有在定义了额外的 TemplateResolver Bean 时才需要设置这个属性。
spring.thymeleaf.template-resolver-order=
#可解析的视图名称列表,用逗号分隔
spring.thymeleaf.view-names=
springboot笔记-thymeleaf的更多相关文章
- springboot笔记06——使用Thymeleaf模板引擎
前言 Springboot 推荐使用Thymeleaf做视图层.Thymeleaf支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式.浏览器解释 html 时会忽略 ...
- SpringBoot 整合thymeleaf
1.Thymeleaf介绍(官网推荐:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html) Thymeleaf是跟Veloc ...
- Springboot+JPA+Thymeleaf 校园博客完整小网站
本文所属[知识林]:http://www.zslin.com/web/article/detail/35 此项目是一个比较简易的校园博客.麻雀虽小五脏俱全,虽然是比较简易的但是涉及的知识点还是比较全面 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础授权权限
上一篇<[原]无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限>介绍了实现Shiro的基础认证.本篇谈谈实现 ...
- 【原】无脑操作:IDEA + maven + Shiro + SpringBoot + JPA + Thymeleaf实现基础认证权限
开发环境搭建参见<[原]无脑操作:IDEA + maven + SpringBoot + JPA + Thymeleaf实现CRUD及分页> 需求: ① 除了登录页面,在地址栏直接访问其他 ...
- 【Springboot】Springboot整合Thymeleaf模板引擎
Thymeleaf Thymeleaf是跟Velocity.FreeMarker类似的模板引擎,它可以完全替代JSP,相较与其他的模板引擎,它主要有以下几个特点: 1. Thymeleaf在有网络和无 ...
- 从.Net到Java学习第九篇——SpringBoot下Thymeleaf
从.Net到Java学习系列目录 Thymeleaf概述 Thymeleaf 是一个流行的模板引擎,该模板引擎采用java语言开发.模板引擎是一个技术名称,是跨领域平台的概念,在java语言体系下有模 ...
- 从.Net到Java学习第六篇——SpringBoot+mongodb&Thymeleaf&模型验证
SpringBoot系列目录 SpringBoot整合mongodb MongoDB 是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.如果你没用过Mong ...
- SpringBoot 之Thymeleaf模板.
一.前言 Thymeleaf 的出现是为了取代 JSP,虽然 JSP 存在了很长时间,并在 Java Web 开发中无处不在,但是它也存在一些缺陷: 1.JSP 最明显的问题在于它看起来像HTML或X ...
随机推荐
- Tarjan求LCA胡乱写的板子 x
首先Tarjan算法的基本思路: 1.任选一个点为根节点,从根节点开始. 2.遍历该点u所有子节点v,并标记这些子节点v已被访问过. 3.若是v还有子节点,继续搜索下去,否则下一步. 4.合并v到u上 ...
- make文件基础用法
参照:https://www.jianshu.com/p/0b2a7cb9a469 创建工作目录,包含一下文件 main.c person.c b.h c.h /*** c.h ***/ //this ...
- win7,win10 系统上搭建testlink1.9.18环境实操步骤
Windows7,10系统上安装TestLink1.9.18(基于xampp) 写于:2018.11.28 二次排版微调:2019.01.01 如遇本文资料缺失,可点击百度网盘查看原始资料. 链接:h ...
- Redis的一点笔记
Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 优势: 性能极高 – Redis能读的速度是110000次/s,写的速度是81000次/s . 丰富 ...
- 剑指offer:关于复制构造函数
1:首先参看代码: #include "stdafx.h" #include "iostream" using namespace std; class A { ...
- Java多线程核心知识
多线程相对于其他 Java 知识点来讲,有一定的学习门槛,并且了解起来比较费劲.在平时工作中如若使用不当会出现数据错乱.执行效率低(还不如单线程去运行)或者死锁程序挂掉等等问题,所以掌握了解多线程至关 ...
- TreeMap元素必须实现Comparable接口
纠正一下,TreeMap实现一定顺序是通过Comparable接口的,而他实现元素不重复也是完全通过compareTo,而不是hashCode和equals,因为debug不会走到hashCode和e ...
- Qt编写大数据大屏UI电子看板系统
前言 目前大屏大数据可视化UI这块非常火,趁热也用Qt来实现一个,Qt这个一站式超大型GUI超市,没有什么他做不了的,大屏电子看板当然也不在话下,有了QSS和QPainter这两个无敌的工具组合,借用 ...
- forms authentication原理
细说ASP.NET Forms身份认证 asp.net 登陆验证 Form表单验证的3种方式 Understanding and Implementing ASP.NET Custom Forms A ...
- spring cloud microservice provider and consumer
MicroService Provider:https://files.cnblogs.com/files/xiandedanteng/empCloud190824.rarMicroService C ...