011-SpringBoot中Thymeleaf------入门

1.认识Thymeleaf

Thymeleaf 的官方网站:http://www.thymeleaf.org
Thymeleaf 官方手册:https://www.thymeleaf.org/doc/tutorials/3.0/thymeleafspring.html

在这里插入图片描述

  • Thymeleaf 是一个流行的模板引擎,该模板引擎采用 Java 语言开发,模板引擎是一个技术名词,是跨领域跨平台的概念,在 Java 语言体系下有模板引擎,在 C#、PHP 语言体系下也有模板引擎,甚至在 JavaScript 中也会用到模板引擎技术,Java 生态下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(国产) 等。
  • Thymeleaf 对网络环境不存在严格的要求,既能用于 Web 环境下,也能用于非 Web 环境下。在非 Web 环境下,他能直接显示模板上的静态数据;在 Web 环境下,它能像 Jsp 一
    样从后台接收数据并替换掉模板上的静态数据。它是基于 HTML 的,以 HTML 标签为载体,Thymeleaf 要寄托在 HTML 标签下实现。
  • SpringBoot 集成了 Thymeleaf 模板技术,并且 Spring Boot 官方也推荐使用 Thymeleaf 来替代 JSP 技术,Thymeleaf 是另外的一种模板技术,它本身并不属于 Spring Boot,Spring Boot只是很好地集成这种模板技术,作为前端页面的数据展示,在过去的 Java Web 开发中,我们往往会选择使用 Jsp 去完成页面的动态渲染,但是 jsp 需要翻译编译运行,效率低

2. Springboot整合Thymeleaf

2.1 创建Module

  • 017-springboot-thymeleaf-first

在这里插入图片描述

  • 添加web依赖

在这里插入图片描述

  • 添加thymeleaf依赖

在这里插入图片描述

  • 按照这种方式创建后,pom.xml 文件下会自动添加如下依赖
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

2.2 在application.properties狠心配置文件中配置thymeleaf

#thymeleaf页面的缓存开关,默认true,表示开启缓存
#建议在开发阶段关闭 thymeleaf 页面缓存,目的实时看到页面
spring.thymeleaf.cache=false

#thymeleaf模板前缀配置
spring.thymeleaf.prefix=classpath:/templates/
#thymeleaf模板后缀配置
spring.thymeleaf.suffix=.html

2.3 创建 ThymeleafControlle 去映射到模板页面

package com.zzy.springboot.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymeleafController {
    /**
     * 入门案例
     * @param model
     * @return
     */
    @RequestMapping(value = "/thymeleaf/index")
    public String index(Model model){
        model.addAttribute("data", "springboot整合thymeleaf入门案例");
        return "index";
    }
}

2,4 在 src/main/resources 的 templates 下新建一个 index.html 页面用于展示数据

  • 文件位置

在这里插入图片描述

  • html标签中添加thymeleaf命名空间

HTML 页面的元素中加入以下属性:
< html xmlns:th=“http://www.thymeleaf.org” >

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <!--Thymeleaf 前端框架以 Html 为载体-->
    <!--获取model中的数据-->
    <span th:text="${data}"></span>
    <span th:text="${data}"></span>
    <p th:text="${data}"></p>
    <div th:text="${data}"></div>
</body>
</html>

2.5 运行主启动类访问

http://localhost:8017/thymeleaf/index

在这里插入图片描述

在这里插入图片描述

2.6 右键->查看页面源代码(检查于元素)

通过 th:text获取的数据存放到标签体中

在这里插入图片描述

注意: Springboot 使 用 thymeleaf 作 为 视 图 展 示 :
约 定 将 模 板 文 件 放 置 在src/main/resource/templates 目录下,
静态资源放置在 src/main/resource/static 目录下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值