Thymeleaf的th标签的应用
1、变量表达式 ${……}
例子:<input type="text" name="userName" value="James Carrot" th:value="${user.name}" />
引用user对象的name属性值。
2、选择/星号表达式 *{……}
例子:<div th:object="${session.user}"> <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p> </div>
选择表达式一般跟在th:object后,直接取object中的属性。
3、文字国际化表达式 #{……}
例子:<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
调用国际化的welcome语句,国际化资源文件如下所示:
resource_en_US.properties: home.welcome=Welcome to here! resource_zh_CN.properties: home.welcome=欢迎您的到来!
4、URL表达式 @{……}
例:1:<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
@{……}支持决定路径和相对路径。其中相对路径又支持跨上下文调用url和协议的引用(//code.jquery.com/jquery-2.0.3.min.js)。
例子2:当URL为后台传出的参数 <img src="../../static/assets/images/qr-code.jpg" th:src="@{${path}}" alt="二维码" />
5、简单数据转换(数字,日期)
例子:
<dt>价格</dt>
<dd th:text="${#numbers.formatDecimal(product.price, 1, 2)}">180</dd>
<dt>进货日期</dt>
<dd th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</dd>
6、字符串拼接
例子:<dd th:text="${'$'+product.price}">235</dd>
7、表单
例子:
<form th:action="@{/bb}" th:object="${user}" method="post" th:method="post"><input type="text" th:field="*{name}"/><input type="text" th:field="*{msg}"/><input type="submit"/> </form>
8、显示页面的数据迭代
//用 th:remove 移除除了第一个外的静态数据,用第一个tr标签进行循环迭代显示<tbody th:remove="all-but-first">//将后台传出的 productList 的集合进行迭代,用product参数接收,通过product访问属性值<tr th:each="product:${productList}">//用count进行统计,有顺序的显示<td th:text="${productStat.count}">1</td><td th:text="${product.description}">Red Chair</td><td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}">$123</td><td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</td></tr><tr><td>White table</td><td>$200</td><td>15-Jul-2013</td></tr><tr><td>Reb table</td><td>$200</td><td>15-Jul-2013</td></tr><tr><td>Blue table</td><td>$200</td><td>15-Jul-2013</td></tr></tbody>
9、条件判断,不能用"<”,">"等符号,要用"lt"等替代
例子:<span th:if="${product.price lt 100}" class="offer">Special offer!</span>
10、根据后台数据选中select的选项
<div class="form-group col-lg-6"><label >性别<span> Sex:</span></label><select th:field="${resume.gender}" class="form-control" th:switch="${resumes.gender.toString()}"data-required="true"><option value="男" th:case="'男'" th:selected="selected" >男</option><option value="女" th:case="'女'" th:selected="selected" >女</option><option value="">请选择</option></select></div>
其余的th 标签应用
一、表达式
1)条件表达式
a)If-then (if) ? (then)
b)If-then-else (if) ? (then) : (else)
c)Default (value) ? : (defaltvalue)
2)文字
a)文本文字 'one text','Another one',……
b)数字文字 0,34,3.0,12.3,……
c)布尔文字 true,flase
d)空文字 null
e)文字标记 one,sometext,main,……
3)文本处理
a)字符串连接 +
b)文字替换 | The name id ${name} |
4)算术表达式
a)基本表达式 +,-,*,/,%
b)减号(一元运算符) -
5)布尔表达式
a)基本表达式 and,or
b)布尔否定(一元运算符) !,not
6)比较和相等
a)比较 >,<,>=,<=(gt,lt,ge,le)
b)相等表达式 ==,!=(eq,ne)
二、表达式基本对象
在上下文变量评估OGNL表达式时,一些对象表达式可获得更高的灵活性。这些对象将由#号开始引用。
- #ctx: 上下文对象.
- #vars: 上下文变量.
- #locale: 上下文语言环境.
- #httpServletRequest: (仅在web上文)HttpServletRequest 对象.
- #httpSession: (仅在web上文) HttpSession 对象.
--表达式功能对象
- #dates:java.util.Date对象的实用方法。
- #calendars:和dates类似, 但是 java.util.Calendar 对象.
- #numbers: 格式化数字对象的实用方法。
- #strings: 字符创对象的实用方法: contains, startsWith, prepending/appending等.
- #objects: 对objects操作的实用方法。
- #bools: 对布尔值求值的实用方法。
- #arrays: 数组的实用方法。
- #lists: list的实用方法。
- #sets: set的实用方法。
- #maps: map的实用方法。
- #aggregates: 对数组或集合创建聚合的实用方法。
- #messages: 在表达式中获取外部信息的实用方法。
- #ids: 处理可能重复的id属性的实用方法 (比如:迭代的结果)。
--给特定的属性设值
下面是用th:action给action设值。
<form action="subscribe.html" th:action="@{/subscribe}">
三、还有很多这样的属性,它们每一个都针对一个特定的XHTML或者HTML5属性:
1、th:text="${data}"
将data的值替换该属性所在标签的body。字符常量要用引号,比如th:text="'hello world'",th:text="2011+3",th:text="'my name is '+${user.name}"
2、th:utext和th:text的区别是"unescaped text"。
3、th:with
定义变量,th:with="isEven=${prodStat.count}%2==0",定义多个变量可以用逗号分隔。
4、th:attr
设置标签属性,多个属性可以用逗号分隔,比如th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。
5、th:[tagAttr]
设置标签的各个属性,比如th:value,th:action等。可以一次设置两个属性,比如:th:alt-title="#{logo}"
对属性增加前缀和后缀,用th:attrappend,th:attrprepend,比如:th:attrappend="class=${' '+cssStyle}"
对于属性是有些特定值的,比如checked属性,thymeleaf都采用bool值,比如th:checked=${user.isActive}
6、th:each
循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size,current,even,odd,first,last等属性,如果没有显示设置状态变量, thymeleaf会默 认给个“变量名+Stat"的状态变量。
7、th:if or th:unless
条件判断,支持布尔值,数字(非零为true),字符,字符串等。
8、 th:switch,th:case
选择语句。 th:case="*"表示default case。
Thymeleaf的th标签的应用相关推荐
- thymeleaf模板+Shiro标签对按钮权限的控制
thymeleaf模板+Shiro标签对按钮权限的控制 环境介绍 SpringBoot+Shiro+thymeleaf模板+Layui(前端) 这两天学习了shiro相关的技术,碰到一个问题:如何根据 ...
- Thymeleaf常用th标签
Thymeleaf常用th标签 关键字 功能介绍 案例 th:id 替换id <input th:id="'xxx' + ${collect.id}"/> th:tex ...
- thymeleaf中img标签,如果有图片显示图片,没有图片显示默认图片
转载自: 转载thymeleaf中img标签问题 <img class="layui-nav-img" th:src="${(user.avatarUrl != n ...
- springboot+thymeleaf自定义select标签
效果 <sys:dict type="DISEASES_OPTION" name="option_code" class="" /&g ...
- Thymeleaf中each标签遍历list如何获取index
<tr th:each="user,userStat:${users}"> userStat是状态变量,有 index,count,size,current,even, ...
- 循环input、thymeleaf使用a标签传值并跳转
当input循环取值时,可以使用 th:href="${'/admin/diecast?diecastId='+workshop.id}" 来进行获取单次想要的值 后台可以使用 @ ...
- spring thymeleaf 自定义标签
概述 thymeleaf2.1.5自定义标签及自定义属性案例,类似于JSP中的自定义JSTL标签 详细 代码下载:http://www.demodashi.com/demo/10495.html 一. ...
- springBoot,thymeleaf页面中js使用标签
①页面顶部需要先引入thymeleaf依赖 ②添加标签 ③在js中使用其他标签 ④现在filePrefix就是一个变量了,controller传过来的是什么,那它就是什么. 转载于:https://w ...
- SpringBoot整合thymeleaf及常用th:标签使用方法
pom文件加入启动依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId& ...
最新文章
- 吉利蔚来造手机,小米造车
- php二维数组中的查找,PHP实现二维数组中的查找算法小结
- for循环c语言流水灯,巧用数组与for循环为流水灯程序瘦身
- HBase环境搭建60010端口无法访问问题解决方案
- 2011浙大878计算机专业基础扫描版真题
- 分层贝叶斯模型——结构
- 关于银行,有哪些有趣的冷知识
- VMware下Ubuntu图形界面切换到命令行终端模式
- Java 八大排序算法
- iis启动 服务无法在此时接受控制信息。 (异常来自 HRESULT:0x80070425)
- HTML中怎么点击超链接让新页面在另一个窗口打开?
- ESXI6.7集成net55-r8168网卡驱动,支持部分PC
- 计算机组装流程详解,笔记本电脑组装全过程图文详解
- 如何在win7旗舰版下运行汇编的debug、masm、link等
- 深圳监控安装上门服务简介
- WannaCrypt(永恒之蓝) down.bddp.net
- 粗粒度与细粒度的解释
- 中国的程序员为何经常被叫做码农?
- 包含高知漫画家珍贵作品的艺术项目“NAKED Manga Mappin’!”3月6日(周五)起在高知龙马机场展出
- 华硕x580nv拆机_华硕x550v怎么拆机 华硕x550v拆机教程【图文步骤】