Thymeleaf初识与使用

##模板引擎

  1. 除了JSP以外,SpringBoot还提供许多其他的模板引擎,比如freemarker、thymeleaf等。
  2. SpringBoot官方不推荐使用JSP, Thymeleaf是SpringBoot推荐使用的视图层技术。
  3. Thymeleaf的文件默认在src/main/resources/templates路径下。这个目录是一个受保护的目录,不能通过HTTP协议直接访问该目录中的资源,毕竟thymeleaf是一个视图模板,如果可以外部直接访问,会缺少数据渲染的过程,无法正常显示视图逻辑。

##thymeleaf简介

  1. Thymeleaf是用于Web和独立环境的现代服务器端Java模板引擎。
  2. Thymeleaf的模板即是HTML,所以能够直接在浏览器中正确显示.thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

##thymeleaf的使用。

加入thymeleaf启动器

注意:在Thymeleaf技术低版本中,对html标签的要求和xml是一致的。但是不符合HTML常见定义方式。提供更高版本的Thymeleaf相关jar包,可以避免这种问题。在Thymeleaf-dialect2.x和Thymeleaf3.x之后就不再对HTML良构有严格限制了。

   <properties>
        <java.version>1.8</java.version>
        <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
        <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
   </properties>

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

thymeleaf配置(application.yml)

server:
  port: 80
spring:
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    cache: false #热部署文件,页面不产生缓存,及时更新
  servlet:
    multipart:
      max-file-size: 100MB #单个文件最大的体积
      max-request-size: 100MB #单次请求中, 所有文件最大的体积

若想使用Thymeleaf的API,需要在在html标签添加属性

<html lang="en" xmlns:th="http://www.thymeleaf.org">

##Thymeleaf常用API介绍

  1. th:text - 文本显示属性
  2. th:utext - 文本原样输出
  3. th:value - 数据录入属性,相当于HTML标签中的value属性
  4. th:action - 用于定义form表单的提交路径
  5. th:href - 用于定义URL路径的thymeleaf语法
  6. th:src - 用于定义资源路径的thymeleaf语法。通常用于定位图片,JS导入等

##Thymeleaf逻辑控制

if判断

<span th:if="${attr} == 'value'">
    show text
<span>

分支判断

<div th:switch="${attr}">
    <span th:case="value1">show text 1</span>
    <span th:case="value2">show text 2</span>
    <span th:case="value3">show text 3</span>
</div>

循环List

<!-- 语法:   变量名 : ${要循环的集合 attr}     类似java中的foreach -->
<tr th:each="u : ${list}">
    <td th:text="${u.userid}"></td>
    <td th:text="${u.username}"></td>
    <td th:text="${u.userage}"></td>
</tr>

循环Map
迭代Map集合,在迭代Map的时候,如果使用迭代线型集合的方式迭代map,每个循环变量类型是Map.Entry。如果想操作Map.Entry中的key或value,可以进行二次迭代。在Thymeleaf中,将Map.Entry看做是一个集合

<div th:each="m : ${map}">
    <span th:text="${m.key}"></span>
    <span th:each="u : ${m.value}" th:text="${u.name}"></span>
</div>

循环中的状态变量
状态变量,就是为循环过程提供状态依据的。如:循环的索引,数量,计数,奇偶数等。状态变量在循环语法中是通用的

<!-- 语法: 循环变量, 状态变量 : ${attr} -->
<tr th:each="u, var : ${list}">
    <td th:text="${var.index}"></td><!-- 索引,从0开始 -->
    <td th:text="${var.count}"></td><!-- 计数,从1开始 -->
    <td th:text="${var.size}"></td><!-- 集合容量 -->
    <td th:text="${var.even}"></td><!-- 是否为偶数 -->
    <td th:text="${var.odd}"></td><!-- 是否为奇数 -->
    <td th:text="${var.first}"></td><!-- 是否是第一个元素 -->
    <td th:text="${var.last}"></td><!-- 是否是最后一个元素 -->
</tr>

访问作用域

<!-- Request -->
<div th:text="${#httpServletRequest.getAttribute('name')}"></div>
<!-- Session -->
<div th:text="${session.userName}"></div>

URL表达式
Thymeleaf会对请求头进行encode操作。Springmvc在处理请求头参数的时候,会进行decode操作

<a th:href="@{http://www.baidu.com}" th:text="绝对路径"></a>
<img th:src="@{/1.jpg}" style="height: 50px"/>
<a th:href="@{/index}">相对路径 - 相对于应用的根</a>
<a th:href="@{~/index}">相对路径 - 相对于服务器的根</a>
<a th:href="@{index}">相对路径 - 相对于当前路径</a>
<a th:href="@{/params(id=1,name=张三)}">参数传递</a>
<a th:href="@{/restfulParams/{id}(id=2, name=李四)}">Restful传参</a>

thymeleaf字符串操作

<!-- 判断字符串非空 -->
${#strings.isEmpty(str)}
<!-- 是否包含子串 -->
${#strings.contains(all, 'str')}
<!-- 是否以什么开头 -->
${#strings.startsWith(attr, 'pre')}
<!-- 是否以什么结尾 -->
${#strings.endsWith(attr,'suf')}
<!-- 字符串长度 -->
${#strings.length(attr)}
<!-- 找子串索引,不存在返回-1 -->
${#strings.indexOf(attr, 'str')}
<!-- 截取子串 -->
${#strings.substring(attr,begin[,end])}
<!-- 转大写 -->
${#strings.toUpperCase(attr)}
<!-- 转小写 -->
${#strings.toLowerCase(attr)}

thymleaf日期操作

<!-- 格式化,默认Thymeleaf是使用浏览器的首选语言环境进行日期的格式化 -->
${#dates.format(date)}
${#dates.format(date, 'formatter')}
<!-- 获取年 -->
${#dates.year(date)}
<!-- 获取月份 -->
${#dates.month(date)}
<!-- 获取日期 -->
${#dates.day(date)}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值