thymeleaf简单使用 th:text, th:each, th:id ,th:switch, th:inline
本文参考: Thymeleaf教程
thymeleaf使用:
引入
<html xmlns:th="http://www.thymeleaf.org">
thymeleaf标准表达式:
thymeleaf支持多种表达式:
- 变量表达式: ${...}
- 选择变量表达式: *{...}
- 连接表达式: @{...}
- 国际化表达式: #{...}
- 片段引用表达式: ~{...}
- 变量表达式
使用${...}包裹的表达式被称为变量表达式,该表达式具有以下功能:
- 获取对象的属性和方法
- 使用内置的基本对象
- 使用内置的工具对象
使用变量表达式还可以使用内置基本对象,获取内置对象的属性,调用内置对象的方法。 Thymeleaf 中常用的内置基本对象如下:
- #ctx :上下文对象;
- #vars :上下文变量;
- #locale:上下文的语言环境;
- #request:HttpServletRequest 对象(仅在 Web 应用中可用);
- #response:HttpServletResponse 对象(仅在 Web 应用中可用);
- #session:HttpSession 对象(仅在 Web 应用中可用);
- #servletContext:ServletContext 对象(仅在 Web 应用中可用)。
除了能使用内置的基本对象外,变量表达式还可以使用一些内置的工具对象。
- strings:字符串工具对象,常用方法有:equals、equalsIgnoreCase、length、trim、toUpperCase、toLowerCase、indexOf、substring、replace、startsWith、endsWith,contains 和 containsIgnoreCase 等;
- numbers:数字工具对象,常用的方法有:formatDecimal 等;
- bools:布尔工具对象,常用的方法有:isTrue 和 isFalse 等;
- arrays:数组工具对象,常用的方法有:toArray、length、isEmpty、contains 和 containsAll 等;
- lists/sets:List/Set 集合工具对象,常用的方法有:toList、size、isEmpty、contains、containsAll 和 sort 等;
- maps:Map 集合工具对象,常用的方法有:size、isEmpty、containsKey 和 containsValue 等;
- dates:日期工具对象,常用的方法有:format、year、month、hour 和 createNow 等。
thymeleaf属性使用:
th:text 和 th:utext使用:
<div th:text="hello">worlddiv>
<div th:utext="hello" >worlddiv>
结果:
<div th:text="2>1">div>
<div th:utext="2>1">div>
<div th:text="'hello ' + 'world'">div>
<div th:utext="'hello ' + 'world'">div>
<div th:text="${msg}">div>
<div th:utext="${msg}">div>
<div><span>helloworld</span>div>
th:id:
<div id="id1" th:id="thymeleaf-id">div>
控制台结果
th:value:
<input type="text" value="input" th:value="thymeleaf_value"/>
结果
th:object :
object="${user}">
第一:"*{username}">
第二:"*{user.username}">
object="${user}">
第三:"${user.username}">
结果:
th:with :
1 2 <span th:with="a = 'hello world'" th:text="${a}">span>
结果:
th:style:
1 2 <span th:style=" 'color:red;font-size:16px;' ">设置样式span>
结果:
经处理后的html加入了行内样式:
th:onclick:
1 2 <input type="button" value="提交" th:onclick=" 'submit()' " />
经处理后的html:
th:each:
请看:
th:if
th:unless
th:switch :
1 7 <div th:if="'a' == 'b'"> a == bdiv> 8 <div th:if=" 'a' eq 'a'">a eq adiv> 9 <div th:unless=" 1==2 ">th:unless 1==2 div> 10 <div th:with="a = '3'" th:switch="${a}" > 11 <span th:case="1">case = 1span> 12 <span th:case="3">case =3span> 13 <span th:case="2">case =2span> 14 div>
结果:
th:selected
th:src
th:inline:
1 14 <select th:with="a = 3"> 15 <option th:selected="${a==1}">- 1 -option> 16 <option th:selected="${a==2}">- 2 -option> 17 <option th:selected="${a==3}">- 3 -option> 18 select> 19 <img src="http://www.baidu.com" th:src="@{/wode}"/> 20 <script th:with="a='zhangsan' " type="text/javascript" th:inline="javascript"> 21 var name = [[${a}]]; 22 script>
经过解析后的html代码:
th:inline 详情请看: https://blog.csdn.net/fanpeizhong/article/details/80173452
th:fragment 模板布局,类似 JSP 的 tag,用来定义一段被引用或包含的模板片段
th:insert 布局标签;
将使用 th:fragment 属性指定的模板片段(包含标签)插入到当前标签中。
- th:insert="commons/bar::footer">
th:replace 布局标签;
使用 th:fragment 属性指定的模板片段(包含标签)替换当前整个标签。
- th:replace="commons/bar::footer">
th:action 替换表单提交地址
Thymeleaf公共页面抽取请看: