原文:
http://blog.csdn.net/pdw2009/article/details/44700897
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 4Thymeleaf tutorial: exercise 2 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 1: bean values
10Product information
11
- 12
- Product name 13
- Red Chair 14 15
- Product price 16
- 350 17 18
- Product available from 19
- 2014-12-01 20
2.简单数据转换(数字,日期)
1 2 3 4Thymeleaf tutorial: exercise 2 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 2: bean values
10Product information
11
- 12
- Product name 13
- red Chair 14 15
- Product price 16
- 180 17 18
- Product available from 19
- 2014-12-01 20
3.字符串拼接
1 2 3 4Thymeleaf tutorial: exercise 3 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 3: string concatenation
10Product information
11
- 12
- Product price 13
- 235 14
4.国际化
1 2 3 4Thymeleaf tutorial: exercise 4 5 6 7 8 9Thymeleaf tutorial - Solution for exercise 4: internationalization
10Product information
11
- 12
- Product name 13
- Red chair 14 15
- Product price 16
- 350 17 18
- Product available from 19
- 28-Jun-2013 20
此时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 4Thymeleaf tutorial: exercise 5 5 6 7 8 9Thymeleaf tutorial - Solution for exercise 5: escaped and unescaped text
1011 Some escaped text 121314 Some unescaped text 1516 17
上述两个div分别生成的html代码为
This is an HTML text. Enjoy yourself!This is an HTML text. Enjoy yourself!
6.迭代
1 2 3 4Thymeleaf tutorial: exercise 6 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 6: iteration
10Product list
11
Description | 15Price | 16Available from | 17
---|---|---|
Red Chair | 22$123 | 232014-12-01 | 24
White table | 27$200 | 2815-Jul-2013 | 29
Reb table | 32$200 | 3315-Jul-2013 | 34
Blue table | 37$200 | 3815-Jul-2013 | 39
7.迭代统计
1 2 3 4Thymeleaf tutorial: exercise 7 5 6 7 8 9Thymeleaf tutorial - Solution for exercise 7: iteration stats
10Product list
11
Index | 15Description | 16Price | 17Available from | 18
---|---|---|---|
1 | 23Red chair | 24$350 | 2528-Jun-2013 | 26
2 | 29White table | 30$200 | 3115-Jul-2013 | 32
3 | 35Reb table | 36$200 | 3715-Jul-2013 | 38
4 | 41Blue table | 42$200 | 4315-Jul-2013 | 44
8.条件判断
1 2 3 4Thymeleaf tutorial: exercise 8 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 8: conditions
10Product list
11
Description | 15Price | 16Available from | 1718 |
---|---|---|---|
Red chair | 23$350 | 2428-Jun-2013 | 2526 Special offer! 27 | 28
9.更多条件判断
1 2 3 4Thymeleaf tutorial: exercise 9 5 6 7 8 9Thymeleaf tutorial - Answer for exercise 9: more on conditions
10Customer list
11
First name | 15Last name | 16Gender | 17Payment method | 18Balance | 19
---|---|---|---|---|
Peter | 24Jackson | 25 3031 32 33 Unknown 34 | 3536 Direct debit 37 38 39 Payment must be done in the next 4 days 40 41 | 42 43350 | 44
Mary | 47Johanson | 4849 | Credit card | 505000 |