博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Thymeleaf基本知识
阅读量:4473 次
发布时间:2019-06-08

本文共 11218 字,大约阅读时间需要 37 分钟。

Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用。

Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式,因此也可以用作静态建模。你可以使用它创建经过验证的XML与HTML模板。相对于编写逻辑或代码,开发者只需将标签属性添加到模板中即可。接下来,这些标签属性就会在DOM(文档对象模型)上执行预先制定好的逻辑。Thymeleaf的可扩展性也非常棒。你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑。这意味着Thymeleaf还可以作为模板引擎框架。

Thymeleaf的模板还可以用作工作原型,Thymeleaf会在运行期替换掉静态值。例如下面的html文件,当作为静态文件时,product name显示为Red Chair,当运行在容器中并提供product这个对象时,product name的值会自动替换为product.description对应的值。

1.bean值替换
 
1  2  3      4         Thymeleaf tutorial: exercise 2 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 1: bean values

10

Product information

11
12
Product name
13
Red Chair
14 15
Product price
16
350
17 18
Product available from
19
2014-12-01
20
21 22
 
2.简单数据转换(数字,日期)
 
1  2  3      4         Thymeleaf tutorial: exercise 2 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 2: bean values

10

Product information

11
12
Product name
13
red Chair
14 15
Product price
16
180
17 18
Product available from
19
2014-12-01
20
21 22
 
3.字符串拼接
 
1  2  3      4         Thymeleaf tutorial: exercise 3 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 3: string concatenation

10

Product information

11
12
Product price
13
235
14
15 16
 
4.国际化
 
1  2  3      4         Thymeleaf tutorial: exercise 4 5         
6
7 8 9

Thymeleaf tutorial - Solution for exercise 4: internationalization

10

Product information

11
12
Product name
13
Red chair
14 15
Product price
16
350
17 18
Product available from
19
28-Jun-2013
20
21 22
 

此时html需要相应的配置文件。例如如下配置文件:

en:

tutorial.exercise4=Thymeleaf tutorial - exercise 4: internationalizationproduct.info=Product informationproduct.name=Product nameproduct.price=Product priceproduct.available=Product available fromback=Back

fr

tutorial.exercise4=Tutorial De Thymeleaf - exercice 4: l'internationalisationproduct.info=Information du produitproduct.name=Nom du produitproduct.price=Prix du produitproduct.available=Produit disponible depuisback=Revenir
5.转义和非转义文本
 
1  2  3      4         Thymeleaf tutorial: exercise 5 5         
6
7 8 9

Thymeleaf tutorial - Solution for exercise 5: escaped and unescaped text

10
11 Some escaped text12
13
14 Some unescaped text15
16 17
 

上述两个div分别生成的html代码为

This is an <em>HTML</em> text. <b>Enjoy yourself!</b>
This is an
HTML text.
Enjoy yourself!
6.迭代
 
1  2  3      4         Thymeleaf tutorial: exercise 6 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 6: iteration

10

Product list

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Description Price Available from
Red Chair $123 2014-12-01
White table $200 15-Jul-2013
Reb table $200 15-Jul-2013
Blue table $200 15-Jul-2013
42 43
 
7.迭代统计
 
1  2  3      4         Thymeleaf tutorial: exercise 7 5         
6
7 8 9

Thymeleaf tutorial - Solution for exercise 7: iteration stats

10

Product list

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Index Description Price Available from
1 Red chair $350 28-Jun-2013
2 White table $200 15-Jul-2013
3 Reb table $200 15-Jul-2013
4 Blue table $200 15-Jul-2013
47 48
8.条件判断
 
1  2  3      4         Thymeleaf tutorial: exercise 8 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 8: conditions

10

Product list

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
28
29
30
Description Price Available from
Red chair $350 28-Jun-2013 26 Special offer!27
31 32
 
9.更多条件判断
 
1  2  3      4         Thymeleaf tutorial: exercise 9 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 9: more on conditions

10

Customer list

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
30
35
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
60
61
62
63
First name Last name Gender Payment method Balance
Peter Jackson 31 Male
32 Female
33 Unknown34
36 Direct debit37
38 39 Payment must be done in the next 4 days40 41
350
Mary Johanson Credit card 5000
Robert Allen 57 Credit card58 Payment must be done in the next 4 days59 50000
64 65
 
10.Spring表达式语言
 
1  2  3      4         Thymeleaf tutorial: exercise 10 5         
6
7 8 9

Thymeleaf tutorial - Solution for exercise 10: Spring Expression language

10 11

Arithmetic expressions

12

Four multiplied by minus six multiplied by minus two module seven:

13

123

14 15

Object navigation

16

Description field of paymentMethod field of the third element of customerList bean:

17

Credit card

18 19

Object instantiation

20

Current time milliseconds:

21

22-Jun-2013

22 23

T operator

24

Random number:

25

123456

26 27
 
11.超链接
 
1  2  3      4         Thymeleaf tutorial: exercise 11 5         
6
7 8 9

Thymeleaf tutorial - Answer for exercise 11: links

10

Product actions

11
15 16
12.表单
1  2  3      4         Thymeleaf tutorial: exercise 12 5         
6
7 8 9

Thymeleaf tutorial - Solution for exercise 12: forms

10

Customer edition

11
12
13 14
15
16 17
18
19 20 Genre:21
22
23
24
25
26
27
28
29 30
31
37 38
39
40 41
42
43 44
 
13.内联
 
            Thymeleaf tutorial: exercise 13        

Thymeleaf tutorial - Solution for exercise 13: inlining

Birthday email

 

转载于:https://www.cnblogs.com/liuyingke/p/7116173.html

你可能感兴趣的文章
cookie和session
查看>>
C++著名程序库的比较和学习经验(STL.Boost.GUI.XML.网络等等)
查看>>
Spring Boot构建RESTful API与单元测试
查看>>
【JavaScript你需要知道的基础知识~】
查看>>
谷歌搜索语法
查看>>
static 静态变量
查看>>
Java面试题(05)
查看>>
Oracle基础
查看>>
pytest_用例运行级别_模块级
查看>>
HDU多校Round 10
查看>>
JFree图表
查看>>
读/写文件操作
查看>>
20155339平措卓玛 Exp1 PC平台逆向破解(5)M
查看>>
本地项目上传码云
查看>>
TensorFlow在Windows上的CPU版本和GPU版本的安装指南(亲测有效)
查看>>
蒟蒻吃药计划-治疗系列 #round5 采药+数字组合代码存放
查看>>
Git
查看>>
ImageSwitcher 右向左滑动的实现方式
查看>>
数学之美读书笔记一信息的度量和作用
查看>>
《荣枯鉴》示伪卷八
查看>>