springBoot学习之路之thymeleaf模板引擎篇

****1.市面上一般的模板引擎有JSP,velocity,freemaker,thymeleaf
****2.springboot内置的模板引擎是thymeleaf.
thymeleaf:语法更加简单,功能更加强大。
3.thymeleaf-layout是thymeleaf的扩展引擎
注意thymeleaf3.x使用的是thymeleaf-layout2.x
thymeleaf2.x使用的是thymeleaf-layout1.x
使用流程:
3.1 引入thymeleaf
3.2 thymeleaf使用方法&语法
*** *3.2.1thymeleaf的初始化

public class GTVGApplication {

...
private final TemplateEngine templateEngine;
...


public GTVGApplication(final ServletContext servletContext) {

    super();

    ServletContextTemplateResolver templateResolver = 
            new ServletContextTemplateResolver(servletContext);
    
    // HTML is the default mode, but we set it anyway for better understanding of code
    templateResolver.setTemplateMode(TemplateMode.HTML);
    // This will convert "home" to "/WEB-INF/templates/home.html"
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    // Template cache TTL=1h. If not set, entries would be cached until expelled
    templateResolver.setCacheTTLMs(Long.valueOf(3600000L));
    
    // Cache is set to true by default. Set to false if you want templates to
    // be automatically updated when modified.
    templateResolver.setCacheable(true);
    
    this.templateEngine = new TemplateEngine();
    this.templateEngine.setTemplateResolver(templateResolver);
    
    ...

}

}
(**此处代码详见thymeleaf官网:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#the-template-resolver)
–根据上方的初始化代码可以看出,我们只要把我们的html放在classpath:/templates/,这时的thymeleaf就能够自动渲染。
3.2.2thymelaef使用
*导入thymeleaf的名称空间,这样我们在编写代码的时候就会有自动提示(非常好用哦!!)
*使用thymeleaf的语法
简单表达式:
变量表达式: ${…}
选择变量表达式: {…}
${…}表达式实际上是在上下文中包含的变量映射上执行的OGNL(对象图导航语言)表达式。有关OGNL语法和功能的详细信息,您应该阅读《OGNL语言指南
消息表达: #{…}
链接URL表达式: @{…}
片段表达式: ~{…}
文字
文本文字:‘one text’,‘Another one!’,…
号码文字:0,34,3.0,12.3,…
布尔文字:true,false
空文字: null
文字标记:one,sometext,main,…
文字操作:
字符串串联: +
文字替换: |The name is ${name}|
算术运算:
二元运算符:+,-,
,/,%
减号(一元运算符): -
布尔运算:
二元运算符:and,or
布尔否定(一元运算符): !,not
比较和平等:
比较:>,<,>=,<=(gt,lt,ge,le)
等号运算符:==,!=(eq,ne)
条件运算符:
如果-则: (if) ? (then)
如果-则-否则: (if) ? (then) : (else)
默认: (value) ?: (defaultvalue)
特殊令牌:
无操作: _

详情参见:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax

***** 表达工具对象*****
除了这些基本对象之外,Thymeleaf将为我们提供一组实用程序对象,这些对象将帮助我们在表达式中执行常见任务。

#execInfo:有关正在处理的模板的信息。
#messages:用于获取变量表达式内的外部化消息的方法,与使用#{…}语法获得消息的方法相同。
#uris:用于转义部分URL / URI的方法
#conversions:用于执行已配置的转换服务(如果有)的方法。
#dates:java.util.Date对象的方法:格式化,组件提取等。
#calendars:类似于#dates,但用于java.util.Calendar对象。
#numbers:格式化数字对象的方法。
#strings:String对象的方法:包含,startsWith,前置/附加等。
#objects:一般对象的方法。
#bools:布尔值评估的方法。
#arrays:数组方法。
#lists:列表方法。
#sets:套方法。
#maps:地图方法。
#aggregates:用于在数组或集合上创建聚合的方法。
#ids:用于处理可能重复的id属性的方法(例如,由于迭代的结果)

详情参见:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#standard-expression-syntax

4.实例:



在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值