Spring Boot (三)Thymeleaf介绍

简介

  1. 首先介绍下Themeleaf, 是一个模板渲染引擎,在开发单体项目中使用,不适用于前后端分离的项目。使用html标签,先这样,大致先学习一下,再回来总结。
  2. 下面介绍下Themeleaf 基本使用

Themeleaf 基本使用

  1. 创建项目

  2. 修改pom文件,添加Themeleaf启动器依赖

     <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
     </dependency>
    
    
  3. 创建controller类 ThemeleafPagaController
    实现了页面跳转并返回一个字符串值

    package com.example.demo1.Controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class ThemeleafPagaController {
        /**
         * 页面跳转方法
         */
    
        @GetMapping("/show")
        public  String showPage(Model model){
            model.addAttribute("msg","hello Themeleaf");
            return "hello";
        }
    
    }
    
    

4.创建简单视图 hello.html文件

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
   <span th:text = "你好张杰"> </span>
   <hr/>
   <span th:text =" ${msg}"></span>

</body>
</html>
  • 运行效果在这里插入图片描述

themeleaf 语法讲解

  • 下面开始讲解一些基本的themeleaf基本语法

字符串与变量的输出操作

  • 引入命名空间
    在html标签中 加入命名空间
  • th:text 标记 (在页面中输出值)
    • 向一个html标签中渲染一个常量串 (如 “你好张杰”)
    • 将一个变量值渲染到html标签中
      <body>
          <span th:text = "你好张杰"> </span>
          <hr/>
          <span th:text =" ${msg}"></span>
      
      </body>
      
  • th:value 标记 (可以将一个值输入到input标签中的value值)
    <body>
        <span th:text = "你好张杰"> </span>
        <hr/>
        <span th:text =" ${msg}"></span>
    
        <hr/>
        <input th:value="${msg}">
    
    </body>
    
    在这里插入图片描述

字符串操作

  • themeleaf 提供了一些内置对象,,可以直接在模板中使用。以#引用内置对象。
  • 使用内置对象的语法
    1. 引用内置对象需要使用 #
    2. 大部分内置对象的名称都以 s 结尾,如strings,numbers,dates
      (strings的 contains,indexof,length方法此处不再详细介绍了,用法一样)
         <!--判断msg字符串是否为空,为空返回 true,不为空
         返回false-->
         <span th:text="${#strings.isEmpty(msg)}"></span>
      

themeleaf 迭代器遍历

  • th:each 迭代器,用于循环迭代集合。
    • 创建对应的实体类custmors
    • 视图层代码
        <table border="1" width="50%">
          <tr>
              <th>ID</th>
              <th>Name</th>
              <th>age</th>
          </tr>
      
          <tr th:each="u : ${list}">
              <td th:text="${u.id}"> </td>
              <td th:text="${u.name}"> </td>
              <td th:text="${u.age}"> </td>
          </tr>
      
      </table>
      
    • controller层代码
      List<customers> list = new ArrayList<>();
      list.add(new customers("1","admin1",22));
      list.add(new customers("2","admin2",23));
      list.add(new customers("3","admin3",24));
      list.add(new customers("4","admin4",25));
      model.addAttribute("list",list);
      
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
## 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判断字符串是否为空 <p th
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值