thymeleaf入门简介

读音

发音就是Time leaf
有些培训的会念成塞姆来福
其实念啥没问题

定义

Thymeleaf 是一个模板引擎,由于SpringBoot默认使用Thymeleaf,现逐步取代jsp。

特点

动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,无网络显示静态内容,有网络用后台得到数据替换静态内容。支持 html 原型,浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
与SpringBoot完美整合,springboot默认整合thymeleaf。提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

链接

官网
官方文档

依赖

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

使用方式

绑定

把html等文件放在resources的templates下。
在controller文件中写导向

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
  <h1>test?</h1>
</body>
</html>
package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
//templates下的页面需要通过controller进行跳转,使用thymeleaf
public class IndexController {
    @GetMapping("test")
    public String test(){
        return "test";
    }
}

传值

@Controller
//templates下的页面需要通过controller进行跳转,使用thymeleaf
public class IndexController {
    @GetMapping("test")
    public void test(Model model){
        model.addAttribute("msg","hello,Thymeleaf");

    }
}
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>测试</title>
</head>
<body>
  <h1>test?</h1>
  <div th:text="${msg}"></div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值