springboot基础2(Thymeleaf模板引擎)

1、简介

​ Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎 (之前用的jsp也是模版引擎但是比较弱),是springboot官方推荐使用的模板引擎。

2、配置

(1)Maven依赖

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

(2)yaml关闭缓存

​ Thymeleaf默认会开启页面缓存,提高页面并发能力。但会导致我们修改页面不会立即被展现,因此我们关闭缓存。

# 关闭Thymeleaf的缓存
spring:
  thymeleaf:
    cache:
      false

3、基本用法

​ 在html标签内的属性前加th:,大部分属性都支持的。

(1)基本表达式

​ 这里着重介绍变量表达式URL表达式

变量表达式${name}:如果session或model中有名为name的key,就可以取出name的value。

<span th:text="${name}">  

URL表达式@{/…/…},在/templates目录下会把静态资源(css,js,img)过滤,但是如果url由Thymeleaf管理就可以成功访问资源。'/'指的是resources/static目录

<link th:href="@{/css/bootstrap.min.css}"/>

(2)常用标签

  • 文本替换

    <p th:text="${user.name}">name</p>
    
  • 属性赋值

    <input th:value="${user.name}" >
    
  • 替换资源/超链接

    <script type="text/javascript" th:src="@{/index.js}"></script>
    
  • 点击事件

    <p th:onclick="'getCollect()'"></p>
    
  • for-each

    <tbody th:each="it:${list}">
        <tr>
           <th th:text="${it.getId()}" ></th>
           <td th:text="${it.getUsername()}"></td>
       </tr>
     </tbody>
    
  • if

    <a th:if="${userId == collect.userId}" >
    
  • switch-case

    <!-- th:switch / th:case -->
    <div th:switch="${user.name}">
      <p th:case="'admin'">User is an administrator</p>
      <!-- *: case的默认的选项 -->
      <p th:case="*">User is some other thing</p>
    </div>
    
  • 公共页面
    前端其实许多页面组成是重复的,比如侧边栏、头部导航栏、底部导航栏等,为了提高代码复用性可以使用公共页面。

    th:fragment标记此标签为模板,根据名字可以引用模板

    <div class="navMenu" th:fragment="header">
      <div class="navMenuContain">
        导航菜单
      </div>
    </div>
    

    th:replace引用
    表示引用cmment目录下的comment.html的header模板

    <div th:replace="~{comment/comment::headbar}"></div>
    

    th:include引用

    <div th:insert="~{comment/comment::headbar}"></div>
    

    include和replace区别

    th:include:引入子模块的children,依然保留父模块的tag。
    th:replace:引入子模块的所有,不保留父模块的tag。

(3)传参

​ 在连接地址的后面加上(key=‘value’),下面表示点击这个超链接会向/index.html传入名为l的参数,参数值是’参数’。

<a class="btn btn-sm" th:href="@{/index.html(l='参数')}">11111</a>

4、常用函数

​ 这篇博客大佬列举的很全Thymeleaf 常用函数_thymeleaf 函数-CSDN博客

## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 attrRequestScope不为空显示 ~~~attrRequestScope为空显示~~~~ ``` #### 2. 测试循环 ```html 测试循环 ``` > 1. 使用th:each进行集合数据迭代 > 2. th:each="声明变量:${集合}" > 3. th:each 用在哪个标签上,哪个标签就会出现多次 ### 7. 引入外部代码 1. 要被引入的代码 include/part.html ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` 2. 需要引入的页面 ```html ``` 3. 渲染后的页面源码 ```html 被包含的内容1111 被包含的内容2222 被包含的内容3333 ``` > 1. " :: "左边的值拼前后缀后必须能够找到要包含的文件。 > 2. " :: " 右边的值是代码片段的名字 ,就是th:fragment的值。 > 3. insert将代码原样引入。 > 4. replace使用被引入的代码和属性替换原有的。 > 5. include使用被引入的代码div内部的代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值