模板引擎

引入

最早开发的时候,展示页面我们都是使用HTML完成我们的代码编写;但是我们的显示页面一定是需要动态变化的,之后就引入了Jsp技术,用来进行数据的显示及交互,但是Jsp是以war包进行部署,但是之后想用jar包的方式打包,这种方式就会很麻烦,所以就有了模板引擎技术 ,模板引擎有很多,比如jsp,freemarker,thymeleaf等,我们用thymeleaf来举例

参考地址

官网地址:https://www.thymeleaf.org/

github地址:https://github.com/thymeleaf/thymeleaf

中文网站:https://raledong.gitbooks.io/using-thymeleaf/content/

使用

先引入依赖,我用SpringBoot的starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

在springboot中有专门的thymeleaf配置类:ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {

   private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

   public static final String DEFAULT_PREFIX = "classpath:/templates/";

   public static final String DEFAULT_SUFFIX = ".html";

   /**
    * Whether to check that the template exists before rendering it.
    */
   private boolean checkTemplate = true;
    
   ..................................
@Controller
public class RequestController {
    @GetMapping("/request")
    public String request(Model model){
        model.addAttribute("msg","name");
        return "show";
    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1 th:text="${msg}"></h1>
</body>
</html>

标准表达式语法

简单表达式

表达式名字语法用途
变量取值${…}获取请求域、session域、对象等值
选择变量*{…}获取上下文对象值
消息表达式#{…}获取国际化等值
链接URL@{…}生成链接
片段表达式~{…}同jsp:include 作用,引入公共页面片段

常量

  • 文本常量 : '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)
  • If-then-else: (if) ? (then) : (else)
  • 默认值 Default: (value) ?: (defaultvalue)

特殊符号

  • 无操作符 : _

th的常用属性值

th:text/ th:utext :设置当前元素的文本内容,两者的区别在于前者不会转义html标签,后者会。优先级不高:order=7

th:value/ th:src/ th:href:设置当前元素的value值,优先级不高:order=6

th:each:遍历循环元素,和th:text或th:value一起使用。注意该属性修饰的标签位置,详细往后看。优先级很高:order=2

th:if:条件判断,类似的还有th:unlessth:switchth:case。优先级较高:order=3

th:insert/ th:include/ th:replace:代码块引入,三者的区别较大,常用于公共代码块提取的场景。优先级最高:order=1

th:fragment:定义代码块,方便被th:insert引用。优先级最低:order=8

th:object:声明变量,一般和*{}一起配合使用,达到偷懒的效果。优先级一般:order=4

th:attr/ th:attrappend/ th:attrprepend:修改任意属性优先级一般:order=5

所有h5兼容的标签写法:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes

关于属性的优先级

thymeleaf内置方法

此外thymeleaf有很多内置方法,与Java的API类似!

  • strings:字符串格式化方法,常用的Java方法它都有。比如:equals,length,trim,toUpperCase,indexOf等

  • numbers:数值格式化方法,常用的方法有:formatDecimal等

  • bools:布尔方法,常用的方法有:isTrue,isFalse等

  • arrays:数组方法,常用的方法有:toArray,length,isEmpty,contains,containsAll等

  • lists,sets:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等

  • maps:对象方法,常用的方法有:size,isEmpty,containsKey,containsValue等

  • dates:日期方法,常用的方法有:format,year,month,hour,createNow等

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值