Thymeleaf基本语法及使用方法
目录
官网:thymeleaf使用方法
1、thymeleaf简介
2、基本语法
2.1、表达式
2.2、字面量
2.3、文本操作
2.4、数学运算
2.5、布尔运算
2.6、比较运算
2.7、条件运算
3、设置属性值-th:attr
3.1、设置单个值
3.2、设置多个值
3.3、简化写法 th:xxxx
4、迭代(循环遍历)
5、条件运算
6、thymeleaf使用
6.1、引入依赖
6.2、 页面开发demo
官网:thymeleaf使用方法
1、thymeleaf简介
- 官网:Thymeleaf is a modern server-side Java template engine for both web and standalone environments.
- 翻译:Thymeleaf是适用于Web和独立环境的现代服务器端 Java
模板引擎
2、基本语法
Thymeleaf 通过在 html 标签中,增加额外属性来达到“模板+数据”的展示方式。
2.1、表达式
表达式名字 |
语法 |
用途 |
变量取值 |
${...} |
获取请求域、session域、对象等值 |
选择变量 |
*{...} |
获取上下文对象值 |
消息 |
#{...} |
获取国际化等值 |
链接 |
@{...} |
生成链接 |
片段表达式 |
~{...} |
jsp:include 作用,引入公共页面片段 |
2.2、字面量
- 文本值: 'one text' , 'Another one!' ,
- 数字: 0 , 34 , 3.0 , 12.3 ,
- 布尔值: true , false
- 空值: null
- 变量: one,two,.... 变量不能有空格
2.3、文本操作
- 字符串拼接: +
- 变量替换: |The name is ${name}|
2.4、数学运算
运算符: + , - , * , / , %
2.5、布尔运算
- 运算符: and , or
- 一元运算: ! , not
2.6、比较运算
比较: > , < , >= , <= ( gt , lt , ge , le )等式: == , != ( eq , ne )
2.7、条件运算
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)
3、设置属性值-th:attr
3.1、设置单个值
<form action="subscribe.html" th:attr="action=@{/subscribe}"><fieldset><input type="text" name="email" /><input type="submit" value="Subscribe!" th:attr="value=#{subscribe.submit}"/></fieldset>
</form>
3.2、设置多个值
<img src="../../images/gtvglogo.png" th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />
3.3、简化写法 th:xxxx
<input type="submit" value="Subscribe!" th:value="#{subscribe.submit}"/>
<form action="subscribe.html" th:action="@{/subscribe}">
4、迭代(循环遍历)
<tr th:each="prod : ${prods}"><td th:text="${prod.name}">Onions</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
<tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'"><td th:text="${prod.name}">Onions</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
</tr>
5、条件运算
<a href="comments.html"
th:href="@{/product/comments(prodId=${prod.id})}"
th:if="${not #lists.isEmpty(prod.comments)}">view</a>
<div th:switch="${user.role}"><p th:case="'admin'">User is an administrator</p><p th:case="#{roles.manager}">User is a manager</p><p th:case="*">User is some other thing</p>
</div>
6、thymeleaf使用
6.1、引入依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
依赖包会自动进行thymeleaf配置:
1、所有thymeleaf的配置值都在 ThymeleafProperties
2、配置好了 SpringTemplateEngine
3、配好了 ThymeleafViewResolver
4、只需要直接开发页面
页面资源默认存放位置:
6.2、 页面开发demo
注意加入thymeleaf的名称空间:xmlns:th="http://www.thymeleaf.org"
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body>
<h1 th:text="${msg}">哈哈</h1>
<h2><a href="www.atguigu.com" th:href="${link}">去百度</a> <br/><a href="www.atguigu.com" th:href="@{link}">去百度2</a>
</h2>
</body>
</html>
Thymeleaf基本语法及使用方法相关推荐
- [JAVAEE] Thymeleaf 基本语法:常用表达式
Thymeleaf 基本语法 常用表达式 变量表达式 ${ } 使用方法:th:xx = "${ }" 获取对象属性值给 th:xx . 后台代码: Student s=new S ...
- thymeleaf模板html a标签,Thymeleaf常用语法:模板片断
Thymeleaf常用语法:模板片断 系统中的很多页面有很多公共内容,例如菜单.页脚等,这些公共内容可以提取放在一个称为"模板片断"的公共页面里面,其它页面可以引用这个 " ...
- JS window对象 Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL。 语法: location.[属性|方法]...
Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: lo ...
- JS对象 数组排序sort() sort()方法使数组中的元素按照一定的顺序排列。 语法: arrayObject.sort(方法函数)...
数组排序sort() sort()方法使数组中的元素按照一定的顺序排列. 语法: arrayObject.sort(方法函数) 参数说明: 1.如果不指定<方法函数>,则按unicode码 ...
- php 检查语法命令,php语法检查的方法有哪些?(代码示例)
php中语法检查是非常有必要的,如果不检查语法,则在发生错误时无法正常工作,如果同时执行语法检查,则可能不会发生该错误,本篇文章我们就来看一看php语法检查的方法. 我们可以使用lint检查 用lin ...
- 在学习thymeleaf中,给function 方法传Controller传递过来的参数,报错:Uncaught SyntaxError: missing ) after argument list
在学习thymeleaf中,给function 方法传Controller传递过来的参数. 报错:Uncaught SyntaxError: missing ) after argument list ...
- mod函数在计算机中实例,excel MOD函数的使用方法实例 MOD函数的公式语法及使用方法实例...
excel MOD函数的使用方法实例 MOD函数的公式语法及使用方法实例,这个问题是我们很多人都很困惑的一个问题,excel MOD函数的使用方法实例是我们非常常用的一个东西,也是我们工作和学习中必不 ...
- Thymeleaf基础语法
Thymeleaf基础语法 今天学习一下Thymeleaf的基础语法. 1-引入Thymeleaf 首先,将view层页面中的html标签进行修改,然后在View层页面文件的其它标签里使用th:*动态 ...
- 关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08
在使用springBoot的时候一般用templates的包来放置静态资源,对于这个包来说它很特殊,这个包它需要导入thymeleaf的依赖才可以用它来和前后端做交互,下面将介绍一下它的使用方法: 导 ...
最新文章
- 扎克伯格、张一鸣、宿华都公开道歉,AI算法不灵了吗?
- Java高并发编程(七):读写锁、LockSupport、Condition
- Linux sed删除文件注释行并删除空行
- VS编译时使用/去除NuGet管理库
- webx学习(四)——ResourceLoadingService
- Python笔记(6) 数字
- 在Raspberry Pi上轻松设置.NET Core并使用VS Code进行远程调试
- Windows 10 Install SQL Server 2005
- 散列表的开放定址法以及再散列法(C语言)
- java 计算百分比 保留两位小数
- 因果推断英文书单整理及简介
- 时间节点管理的自我反省
- [WDS] Disconnected解决方法
- UART串口通信常用协议对比——rs232、485的区别
- unity碰撞检测识别对象的几种方法,刚体篇
- 资本寒冬,看咕咚、Keep、Feel们如何逆势而上
- 夯实基础——P1830 轰炸III
- 微信小程序--公共组件使用
- linux运行python
- 教你如何解析eth的区块信息,并保存所有的交易记录