SpringBoot学习笔记15 - Thymeleaf

Thymeleaf是一个流行的模板引擎,该模板引擎采用 Java 语言开发。Java 中常见的模板引擎有 Velocity、Freemaker、Thymeleaf 等。不同的模板引擎都会具有自己的特定的标签体系,而 Thymeleaf 以 HTML 标签为载体,在 HTML 的标签下实现对数据的展示。Thymeleaf 本身与 SpringBoot 是没有关系的,但 SpringBoot 官方推荐使用 Thymeleaf 作为前端页面的数据展示技术,SpringBoot 很好地集成了这种模
摘要由CSDN通过智能技术生成

1、简介


Thymeleaf是一个流行的模板引擎,该模板引擎采用 Java 语言开发。

Java 中常见的模板引擎有 Velocity、Freemaker、Thymeleaf 等。不同的模板引擎都会具有自己的特定的标签体系,而 Thymeleaf 以 HTML 标签为载体,在 HTML 的标签下实现对数据的展示。

Thymeleaf 本身与 SpringBoot 是没有关系的,但 SpringBoot 官方推荐使用 Thymeleaf 作为前端页面的数据展示技术。同时,SpringBoot 很好地集成了这种模板技术。

关于Thymeleaf 的详情可访问其官网: http://www.thymeleaf.org

2、Spring Boot集成Thymeleaf


2.1、添加依赖

  • 方法1:通过创建工程添加依赖

    在这里插入图片描述

  • 方法2:通过pom.xml添加配置

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

2.2、在开发阶段关闭thymeleaf缓存

打开application.yml文件并添加如下属性:

spring:
  ...
  thymeleaf:
    cache: false

生产环境建议开启(即默认)

2.3、编写Controller类并传参

在这里插入图片描述
将需要传递的参数传递到Model对象中:

@Controller
public class ThymeleafController {
   
    @RequestMapping("/demo")
    public String thymeleafHandler(Model model) {
   
        model.addAttribute("welcome", "hello thymeleaf");
        // 返回 resources/templates 目录下指定html页面,无需添加后缀
        return "index";
    }
}

2.4、添加index.html页面并测试

在 resources/templates 目录下创建index.html页面,编写界面,并从model对象中读取参数值:

在这里插入图片描述

<!DOCTYPE html>
<!-- 添加thymeleaf命名空间 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
    <p th:text="${welcome}">这里将要显示数据,但这些文字不显示。</p>
</body>
</html>

启动并测试:

在这里插入图片描述

3、Thymeleaf标准表达式


3.1、变量表达式${}

获取从Model或Request对象中设置的参数.。

  • 创建实体类

    package com.blairscott.bean;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class Teacher {
         
        private Integer id;
        private String name;
        private int age;
    }
    
  • Controller传递参数

    @Controller
    public class ThymeleafController {
         
        @RequestMapping("/demo")
        public String thymeleafHandler(Model model) {
         
            model.addAttribute("teacher", new Teacher(1, "zhangsan", 18));
            return "index";
        }
    }
    
  • 创建index.html接受并输出参数

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>index</title>
    </head>
    <body>
        <p th:text="${teacher}">这里将要显示数据,但这些文字不显示。</p>
        <p th:text="${teacher.id}">这里将要显示数据,但这些文字不显示。</p>
        <p th:text="${teacher.name}">这里将要显示数据,但这些文字不显示。</p>
        <p th:text="${teacher.age}">这里将要显示数据,但这些文字不显示。</p>
    </body>
    </html>
    
  • 启动服务并测试

    在这里插入图片描述

3.2、选择表达式*{}

先通过th:object="${teacher}"选择了对象,再通过th:text="*{id}"等选择属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值