thymeleaf教程

本教程涵盖了常见的前端操作,比如,判断,循环,引入模板,常用函数(日期格式化,字符串操作)下拉,js和css中使用,基本可以应对一般场景。

怎么使用?

前端html页面标签中引入如下:

<html xmlns:th="http://www.thymeleaf.org">
  • 1

表达式

  • 简单表达式

    • 可用值表达式(后台设置): ${…}
    • 所有可用值表达式: *{…}

    比如*{name} 从可用值中查找name,如果有上下文,比如上层是object,则查object中的name属性。

  • 消息表达式: #{…} 

    国际化时使用,也可以使用内置的对象,比如date格式化数据

  • 链接表达式: @{…} 
    用来配合link src href使用的语法
  • 片段表达式: ~{…} 
    用来引入公共部分代码片段,并进行传值操作使用的语法。

文字

  • 文本: ‘one text’,’another text’,…
  • 数字: 1,2,1.2,…
  • 布尔: true,false
  • 空值:null
  • 单词: something,main,name,…

文本操作

  • 链接:+
  • 替换:|The name is ${name}| 
    html 
    <a href="" th:href="@{|/name/${test.size()}|}">链接地址:</a> 
    //渲染后的结果 
    <a href="/name/3">链接地址:</a> 

数学操作

  • 二元操作:+, - , * , / , %
  • 一元操作: - (负)

布尔操作

  • 一元 : and or
  • 二元 : !,not

比较

  • 比较:> , < , >= , <= ( gt , lt , ge , le )
  • 等于:== , != ( eq , ne )

条件

  • If-then: (if) ? (then)
  • If-then-else: (if) ? (then) : (else)
  • Default: (value) ?: (defaultvalue)

无操作 
使用_ 来禁止转义。

支持的操作

html5的操作支持:

th:abbr          th:accept             th:accept-charset
th:accesskey             th:action             th:align
th:alt             th:archive             th:audio
th:autocomplete             th:axis             th:background
th:bgcolor             th:border             th:cellpadding
th:cellspacing             th:challenge             th:charset
th:cite             th:class             th:classid
th:codebase             th:codetype             th:cols
th:colspan             th:compact             th:content
th:contenteditable             th:contextmenu             th:data
th:datetime             th:dir             th:draggable
th:dropzone             th:enctype             th:for
th:form             th:formaction             th:formenctype
th:formmethod             th:formtarget             th:fragment
th:frame             th:frameborder             th:headers
th:height             th:high             th:href
th:hreflang             th:hspace             th:http-equivth:icon             th:id             th:inline
th:keytype             th:kind             th:label
th:lang             th:list             th:longdesc
th:low             th:manifest             th:marginheight
th:marginwidth             th:max             th:maxlength
th:media             th:method             th:min
th:name            th:onabort            th:onafterprint
th:onbeforeprint            th:onbeforeunload            th:onblur
th:oncanplay            th:oncanplaythrough            th:onchange
th:onclick            th:oncontextmenu            th:ondblclick
th:ondrag            th:ondragend            th:ondragenter
th:ondragleave            th:ondragover            th:ondragstart
th:ondrop            th:ondurationchange            th:onemptied
th:onended            th:onerror            th:onfocus
th:onformchange            th:onforminput            th:onhashchange
th:oninput            th:oninvalid            th:onkeydown
th:onkeypress            th:onkeyup            th:onload
th:onloadeddata            th:onloadedmetadata            th:onloadstart
th:onmessage            th:onmousedown            th:onmousemove
th:onmouseout            th:onmouseover            th:onmouseup
th:onmousewheel            th:onoffline            th:ononline
th:onpause            th:onplay            th:onplaying
th:onpopstate            th:onprogress            th:onratechange
th:onreadystatechange            th:onredo            th:onreset
th:onresize            th:onscroll            th:onseeked
th:onseeking            th:onselect            th:onshow
th:onstalled            th:onstorage            th:onsubmit
th:onsuspend            th:ontimeupdate            th:onundo
th:onunload            th:onvolumechange            th:onwaiting
th:optimum            th:pattern            th:placeholder
th:poster            th:preload            th:radiogroup
th:rel            th:rev            th:rows
th:rowspan            th:rules            th:sandbox
th:scheme            th:scope            th:scrolling
th:size            th:sizes            th:span
th:spellcheck            th:src            th:srclang
th:standby            th:start            th:step
th:style            th:summary            th:tabindex
th:target            th:title            th:type
th:usemap            th:value            th:valuetype
th:vspace            th:width            th:wrapth:vspace            th:width            th:wrap
th:xmlbase            th:xmllang            th:xmlspace

布尔类型

th:async th:autofocus th:autoplay
th:checked th:controls th:declare
th:default th:defer th:disabled
th:formnovalidate th:hidden th:ismap
th:loop th:multiple th:novalidate
th:nowrap th:open th:pubdate
th:readonly th:required th:reversed
th:scoped th:seamless th:selected

判断操作

thymeleaf提供了几种判断,if、switch

  • 后台数据
public class TestVo {private String name;private  Integer Score;private  Integer male;private Date birthday;public TestVo(String name, Integer score, Date birthday, Integer male) {this.name = name;Score = score;this.male = male;this.birthday = birthday;}
@RequestMapping("/test01")public String thymeleaf(ModelMap map){List<TestVo> testVos=new ArrayList<>();testVos.add(new TestVo("数学",10,new Date(),1));testVos.add(new TestVo("数学0001",70,new Date(),2));testVos.add(new TestVo("数学01",100,new Date(),3));map.put("test",testVos);return "/back/import/test";}
  • 前端语法
  <table><thead><th></th></thead><tbody><tr th:each="test:${test}"><!--判断成绩--><td th:if="${test.Score} gt 0 and ${test.Score} lt 60">差</td><td th:if="${test.Score} ge 60 and ${test.Score} le 70">中</td><td th:if="${test.Score} gt 70 and ${test.Score} le 80">良</td><td th:if="${test.Score} gt 80 and ${test.Score} le 90">优</td><td th:if="${test.Score} gt 90 and ${test.Score} le 100">超级优秀</td></tr><br><tr th:each="test:${test}"><!--判断成绩   一般只有两种情况的时候可以使用这种方式--><td th:if="${test.Score} gt 0 and ${test.Score} lt 60">差</td><!--除了这些条件之外的--><td th:unless="${test.Score} gt 0 and ${test.Score} lt 60">及格</td></tr><tr th:each="test:${test}"><td th:switch="${test.male}"><span th:case="1">男</span><span th:case="2">女</span><!--其他情况--><span th:case="*">未知</span></td></tr></tbody></table>
  • 结果

差 
中 
超级优秀

差 
及格 
及格

男 

模板操作

主要引入公共头部和底部相关代码使用 ,当然也可以其他地方使用 
- 示例

底部模板代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<body>
<div th:fragment="footer">© 2016 xxx
</div>
</body>
</html>

Springboot整合引入模块

<!--写入绝对路径即可引入 -->
<div th:include="/back/import/footer :: footer"></div>

文本和带格式文本

用来输入内容到标签内部,而不是attr中。分为th:text和th:utext两种,后者可以渲染文本中的标签。 
- th:utext

    map.put("msgutext","<b>1111</b>");
  • 1
<div th:utext="${msgutext}"></div>

结果:被渲染了

  • th:text
<div th:text="${msgutext}"></div>

结果:原样输出到页面。

外围包裹–block

有时候需要在代码外部加层条件,但写div之类的又影响样式,此情况下你可以用下面这种方式:

    <th:block th:with="score=${test.Score}"><td th:if="${score} ge 60">及格啦</td></th:block>

循环

遍历元素

  • 示例:
 <tr th:each="test:${test}"><td th:text="${test.Score}"></td></tr>

循环下标判断

 List<String> list=new ArrayList<String>();list.add("1s");list.add("2s");list.add("3s");map.put("list",list);
<th:block th:each="mylist,iterStat:${list}">111<span th:text="${mylist}"></span><th:block th:if="${iterStat.index le 1}"><span th:text="${mylist}"></span></th:block>
</th:block>

常用操作

  • 日期格式化
 <td th:text="${#dates.format(content.createDate,'yyyy-MM-dd HH:mm:ss')}"  ></td>
  • 字符截取长度
<td th:if="${#strings.length(content.title) gt 5 } "  th:text="${#strings.substring(content.title,0,5) + '…'}"></td>
  • 下拉选择
 <select name="subId" id="subId" lay-verify="" ><option value="">请选择</option><option th:each="channelsub:${subchannels}" th:selected="${channelsub.id == subId}"    th:value="${channelsub.id}" th:text="${channelsub.name}"></option></select>

js取值

有时候需要传递参数到js中,按如下方式:

  <script th:inline="javascript"  >var size= [[${test.size()}]];console.info(size)</script>
  • 进阶 
    ps: 下面涉及到thymeleaf的语法出,可以替换成其他thymeleaf的语法来用
 <script th:inline="javascript"  >//尺寸等于3时打印日志../*[# th:if="${test.size() eq 3}"]*/console.info('Welcome admin');/*[/]*/</script>

css取值

首先需要后台设置classname、align的值,之后按如下方式:

<style th:inline="css">
.[[${classname}]] {
text-align: [[${align}]];
}
</style>

结语

thymeleaf还有很多其他的语法,这里只是做个入门,方便上手,详细教程请参阅 官方教程。当然也可以加群交流。

thymeleaf 语法大全相关推荐

  1. DB2数据库SQL语法大全推荐word版

    <DB2数据库SQL语法大全推荐word版> 下载地址: 网盘下载 转载于:https://www.cnblogs.com/long12365/p/9731437.html

  2. html设置边框dw,Dreamweaver表格边框设置的css语法大全

    Dreamweaver表格边框设置的css语法大全 具体内容包括:上边框宽度.右边框宽度.下边框宽度.左边框宽度.边框宽度.边框颜色.边框样式.上边框.下边框.左边框.右边框.边框.宽度.高度.有关标 ...

  3. Mysql语法大全(命令行)(简洁、明了、全面)

    Mysql命令行的语法 另外一个Mysql语法大全的版本:点这里 两个Mysql语法大全以及命令行代码!!!基本上学习Mysql没有问题 嘿嘿!学习是一个长期的过程!!!还会继续更新!!! 基本的My ...

  4. thymeleaf 语法——th:text默认值、字符串连接、th:attr、th:href 传参、th:include传参、th:inline 内联、th:each循环、th:with、th:if

    文章目录 1.表达式 1.1.简单表达式 1.2.三元运算.条件表达式: 1.3.默认表达式 1.4.综合使用,属性值的 if - else 2.字符串连接.拼接 3.th:attr 的使用 4.th ...

  5. 批处理文件语法大全(怎么写Bat文件)

    批处理文件语法大全(怎么写Bat文件) 作者:本站  来源:本站整理  发布时间:2006-11-27 17:05:08  发布人:admin 减小字体 增大字体 扩展名是bat(在nt/2000/x ...

  6. Markdown语法大全(超级版)

    Markdown语法大全(超级版) CSDN的参考文档并不全-- 文章目录 Markdown语法大全(超级版) 概述 设计理念 内联 HTML 语法 特殊字符自动转义 行内语法讲解 注释的表述 代码法 ...

  7. Markdown编辑器语法大全

    一.什么是Markdown? Markdown是一种轻量级标记语言,创始人为约翰·格鲁伯(英语:John Gruber).它允许人们使用易读易写的纯文本格式编写文档,然后转换成有效的XHTML(或者H ...

  8. 语法大全!20张图,彻底分清英语中的主、谓、宾、定、状、补

    语法大全!20张图,彻底分清英语中的主.谓.宾.定.状.补 前言:语法是理解英语逻辑的核心.语法不好的同学,处理简单句尚可:遇到复杂句型时,就晕头转向了.所以,要彻底读懂英语,必须在语法上下苦功夫.今 ...

  9. 网页设计HTML经典语法大全

    网页设计HTML经典语法大全 非常实用,写网页时可以当字典查找. ◆<! - - ... - -> 批注 ◆<!> 跑马灯 ◆<MARQUEE>...</MA ...

  10. Typora语法大全(包含数学公式)

    Typora语法大全(含详细数学表达式及流程图) 一.Markdown基本语法 速成教程 标题 #一级标题 (快捷键Ctrl+1) ##二阶标题 (快捷键Ctrl+2) ###三阶标题 (快捷键Ctr ...

最新文章

  1. 官方抓虫,PyTorch 新版本修复 13 项 Bug
  2. 经验教训 | STM32 异常之BFARVALID,PRECISERR,INVSTATE
  3. chrome 插件 vimium 介绍
  4. The J2EE Architect's Handbook
  5. 用numpy操作矩阵,上三角,下三角矩阵,对角化矩阵
  6. Linus 在圣诞节想提前放假做了这些解释,哈哈哈
  7. parallelstream启动的线程数_高并发与多线程网络学习笔记(三)线程组和线程池
  8. ASP.NET Web API 简介
  9. json string 格式_python中json函数的用法
  10. Java中使用foreach带来的一些问题
  11. C#中new的用法,及与override的区别
  12. 通过javaBean反射转换成mybatis映射文件
  13. 二分图最大权匹配:Kuhn-Munkres算法
  14. 信息安全管理体系ISO27001
  15. QPS、TPS、PV、UV、GMV、IP、RPS的概念解释
  16. gmail上不去的解决方法(原)
  17. 【JZOJ4587】Snow的追寻
  18. 《修C传》——初始C语言 <凝气篇>
  19. vue+koa2移动电商实战
  20. wps的ppt怎么存html,如何将网页快速转换为WPS与WORD文档 ppt怎么转换成word文档

热门文章

  1. Reflector、ReflectorFactory的前世
  2. uni-app 编译成H5页面运行,提示“连接服务器超时,请点击屏幕重试”
  3. 51单片机——ADC模数转换、DAC数模转换PWM C语言入门编程
  4. UVA 12307 Smallest Enclosing Rectangle
  5. 【spark2】【源码学习】【环境编译】在idea导入spark源码,执行example并debug
  6. 170402网摘题目
  7. 其实带宽≠速度 ADSL加速法完全功略
  8. 中关村标协智能物联分技术委员会成立,小米张明当选第一届主任委员
  9. 类和对象7:构造和析构方法
  10. google jib容器打包工具