thymeleaf介绍和基本语法使用(代码片段)

tweixi tweixi     2022-12-11     797

关键词:

Spring Boot中推荐使用Thymeleaf作为模板引擎.因为Thymeleaf提供了完美的SpringMVC支持.

Thymeleaf是一个java类库,他是一个xml/xhtml/html5的模板引擎,可以作为mvcweb应用的view层。

技术图片

 

 

 我们把HTML页面放在classpath:/templates/thymeleaf就能自动渲染;

技术图片

 

导入thymeleaf的名称空间

 

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4 <meta charset="UTF‐8">
 5 <title>Title</title>
 6 </head>
 7 <body>
 8 <h1>成功!</h1>
 9 <!‐‐th:text 将div里面的文本内容设置为 ‐‐>
10 <div th:text="$hello">这是显示欢迎信息</div>
11 </body>
12 </html>

 语法规则

 th:text;改变当前元素里面的文本内容

 th:任意html属性;来替换原生属性的值

th:include:加载模板的内容: 读取加载节点的内容(不含节点名称),替换div内容

th:replace:替换当前标签为模板中的标签,加载的节点会整个替换掉加载他的div

th:attr 来设置任意属性

th:attrprepend 来追加(不是替换)属性值

th:classappend

th:each每次遍历都会生成当前这个标签

 th:href="@..." 替换url

 th:text="$..."     转译特殊字符,特殊符号原方不动输出

th:utext="$...    会转译字符,特殊符号被转译后输出结果

 行内写法

[[ ]]等价于th:text      

[( )]等价于th:utext 

 

表达式语法

 选择变量表达式: *...

  消息表达式: #...

 URL 表达式: @...

代码段表达式: ~...

字面量

文本字面量: ‘some text‘

 数值字面量: 0, 34, 3.0, 12.3

 布尔值字面量: true, false

 Null 值字面量: null

 Tokens 字面量: one, content, sometext, ...

文本操作符

 字符串连接: +

字面量替换: |The name is $name|

 算术操作符

 二元操作符: +, -, *, /, %

 减号(一元操作符): -

 布尔操作符(逻辑操作符)

 二元操作符: and, or

 (一元操作符): !, not

 比较操作符

比较: >, <, >=, <= (gt, lt, ge, le)

 相等性: ==, != (eq, ne)

 条件操作符

if-then: (if) ? (then)

if-then-else: ‘(if) ? (then) : (else)‘

默认: (value) ?: (defaultvalue)

特殊符号

 忽略 Thymeleaf 操作: _

内置基本对象

 ctx:

 vars

 locale

 request

 response

 session

 servletContext

内置工具对象

execInfo

messages

uris

conversions

dates

calendars

numbers

strings

objects

bools

arrays

lists

sets

maps

aggregates

ids

Link url

 th:href="@/"返回首页

 

th:href="@/thymeleaf/demo1"跳转demo1 页面

 

th:href="@/thymeleaf/demo1(username=$employees[0].name)">demo1 页面, 带参数

 

th:href="@/thymeleaf/demo1/empId(empId=$employees[1].id)">demo1 页面, RESTful 风格参数

 

Thymeleaf全部标签

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:frame

th:frameborder

th:headers

th:height

th:high

th:href

th:hreflang

th:hspace

th:http-equiv

th:icon

th:id

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: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:wrap

th:xmlbase

th:xmllang

th:xmlspace

 

 


模板引擎thymeleaf介绍及使用(代码片段)

...擎2.1模板引擎介绍2.2模板引擎的作用2.3常见的模板引擎3.Thymeleaf3.1Thymeleaf介绍3.2Thymeleaf语法规则3.2.1标准表达式语法3.2.2th属性3.3Thymeleaf使用流程1.服务器生成动态页 查看详情

模板引擎thymeleaf介绍及使用(代码片段)

...擎2.1模板引擎介绍2.2模板引擎的作用2.3常见的模板引擎3.Thymeleaf3.1Thymeleaf介绍3.2Thymeleaf语法规则3.2.1标准表达式语法3.2.2th属性3.3Thymeleaf使用流程1.服务器生成动态页 查看详情

thymeleaf基础语法(代码片段)

Thymeleaf基础语法今天学习一下Thymeleaf的基础语法。1-引入Thymeleaf首先,将view层页面中的html标签进行修改,然后在View层页面文件的其它标签里使用th:*动态处理页面。2-输出内容使用th:text和th:utext将文本输出道到所在标签的... 查看详情

java基础总结之thymeleaf详解(代码片段)

一、Thymeleaf语法1.1变量表达式$…1.2选择变量表达式*…1.3消息表达式#…1.4链接表达式@…1.5片段表达式~…二、基本使用2.1Thymeleaf模板基本配置一、Thymeleaf语法标签在HTML页面上使用Thymeleaf标签,Thymeleaf标签能够动态地替换掉... 查看详情

thymeleaf---基础知识(代码片段)

Thymeleaf---基础知识1.Thymeleaf菜鸟教程2.Thymeleaf2.1Thymeleaf简介2.2Thymeleaf的使用2.3Thymeleaf基本语法2.3.1在thymeleaf模板页面引⼊th标签的命名空间2.3.2th:text2.3.3th:inline内联2.3.4th:object和*2.4流程控制2.4.1th:each循环2.4.2分支2.5碎片使用2.5. 查看详情

day08-2-thymeleaf(代码片段)

服务器渲染技术-Thymeleaf1.基本介绍官方在线文档:Readonline文档下载:Thymeleaf3.1PDF,EPUB,MOBIThymeleaf是什么Thymeleaf是一个现代的服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CSS甚至纯文本Thymeleaf是一个跟V... 查看详情

react语法基本使用介绍(代码片段)

一、认识入口文件React中,app/main.js叫做入口文件,这个文件是webpack.congifg.js的entry标识的文件。任何项目入口文件只能有1个。入口文件写法:importReactfrom"react";importReactDomfrom"react-dom";importAppfrom"./App";ReactDom.render(<App/>,document 查看详情

一个小demo熟悉springboot和thymeleaf的基本使用(代码片段)

...错误页面7、阶段演示效果介绍此Demo是为了熟悉SpringBoot和thymeleaf的使用,所以适用于刚接触SpringBoot不久的新手,此项目是一个简单的Web版的员工CRUD,项目内容来源于尚硅谷谷粒学院SpringBoot核心技术篇,我根据自己的理解,又重... 查看详情

thymeleaf添加语法提示(代码片段)

thymeleaf添加语法提示:xmlns:th="http://www.thymeleaf.org"  查看详情

thymeleaf模板引擎学习(代码片段)

...SpringBoot中已经不推荐使用JSP页面进行页面渲染了。从而Thymeleaf提供了一个用于整合SpringMVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP,或其他模板引擎,如Velocity、FreeMarker等。它的语法与我们以前使用的EL表达... 查看详情

matlab基本语法(代码片段)

...用(如.\\)矩阵生成矩阵生成向量生成或子阵提取本节将会介绍一些MATLAB的基本语法的使用。持续更新。。。 在MATLAB环境下进行的操作就像是使用一个超级复杂的计算器,不要被这吓到了。在您开始使用MATLAB时可以在“>&g... 查看详情

thymeleaf常用语法:自定义数据转换类(代码片段)

...板文件中,可以使用“$...”表达式进行数据转换,Thymeleaf会使用配置好的数据转换类,来实现转换。例如一个User对象,简单起见假设有姓名和年龄两个字段,对象的toString()方法拼接所有字段,使用“$user”会调用对... 查看详情

jsonpath语法介绍和使用场景(代码片段)

文章目录一、什么是JSONPath二、语法介绍三、哪里可以用的到呢?1.Java2.PHP3.JavaScript4.Python5.Go四、总结一、什么是JSONPathJSONPath之于JSON,就如XPath之于XML。JSONPath可以方便对JSON数据结构进行内容提取。下面是一个JSONPath语法... 查看详情

jsonpath语法介绍和使用场景(代码片段)

文章目录一、什么是JSONPath二、语法介绍三、哪里可以用的到呢?1.Java2.PHP3.JavaScript4.Python5.Go四、总结一、什么是JSONPathJSONPath之于JSON,就如XPath之于XML。JSONPath可以方便对JSON数据结构进行内容提取。下面是一个JSONPath语法... 查看详情

list介绍和基本使用(代码片段)

文章目录一.list介绍二.list使用constructoriteratorcapacityemptysizeElementaccessfront/backModifiersassignpush_front/pop_frontpush_back/pop_backinserteraseswapresizeclearoperationsspliceremoveremove_ifuniquesortr 查看详情

list介绍和基本使用(代码片段)

文章目录一.list介绍二.list使用constructoriteratorcapacityemptysizeElementaccessfront/backModifiersassignpush_front/pop_frontpush_back/pop_backinserteraseswapresizeclearoperationsspliceremoveremove_ifuniquesortr 查看详情

尚融宝06-ecmascript基本介绍和使用(代码片段)

目录一、ECMAScript1、ECMA2、ECMAScript3、什么是ECMA-2624、ECMA-262历史5、ECMAScript和JavaScript的关系二、基本语法1、let声明变量2、const声明常量3、解构赋值4、模板字符串5、声明对象简写6、定义方法简写7、参数的默认值8、对象拓展运算... 查看详情

ssm框架+thymeleaf实现基本的增删改查(代码片段)

前言本文使用了SSM框架、thymeleaf和jquery实现了基本的增删改查。 名词解释SSM框架:springMVC、spring、mybatisthymeleaf:一个与Velocity、FreeMarker类似的模板引擎jquery:一个快速、简洁的JavaScript框架 程序结构 本程序框架是用m... 查看详情