Thymeleaf一篇文章学会使用

简介: Thymeleaf一篇文章学会使用

Thymeleaf讲解

简介

简介:

Thymeleaf 是新一代 Java 模板引擎,与 Velocity、FreeMarker 等传统 Java 模板引擎不同,Thymeleaf 支持 HTML 原型,其文件后缀为“.html”,因此它可以直接被浏览器打开,此时浏览器会忽略未定义的 Thymeleaf 标签属性,展示 thymeleaf 模板的静态页面效果;当通过 Web 应用程序访问时,Thymeleaf 会动态地替换掉静态内容,使页面动态显示。

创建项目

再setting的Editor中的File and code Templates中创建,thymeleaf模板,方便以后的调用。

我们再点击新建的时候,就有了这个thymeleaf选项。

编码

基础使用

代码一(th:text)

第一个语法通过,运行结果进行讲解。

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">默认的title</title>
</head>
</html>  

通过运行结果可以看出,当没有后端给前端发送数据的时候,这个前端显示的信息就是规定的默认信息。

现在加一个后端IndexController

package com.example.thymeleafdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("title", "传递的Title");
        return "index";
    }
}

运行结果

通过运行的结果和观察源码,我们都可以发现,这个后端传递给前端的值,在前端生效了。

代码二(th:content)

index.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">默认的title</title>
<!--    字符串拼接-->
    <meta th:content="|李华的-${description}|" name="description" content="默认的description">
    <meta th:content="${keywords}" name="keywords" content="默认的keywords">
</head>
</html>  

IndexController

package com.example.thymeleafdemo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("title", "传递的Title");
        model.addAttribute("description", "传递的description");
        model.addAttribute("keywords", "传递的keywords");
        return "index"; // 对于html文件可以不写后缀名
    }
}

运行结果

通过这个运行结果,我们可以发现,这个content中的内容也更改了。

常用方法

这里会演示下面这几个的用法

  • th:text
  • th:if
  • th:object
  • th:each
  • th:switch

User

package com.example.thymeleafdemo.Bean;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
public class User {
    private String username;
    private String password;
    private Integer sex;
    private Boolean isVip;
    private Date birthday;
    private List<String> hobbys;
}

IndexController

package com.example.thymeleafdemo.controller;
import com.example.thymeleafdemo.Bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@Controller
public class IndexController {
    @GetMapping("/index")
    public String index(Model model){
        model.addAttribute("title", "传递的Title");
        model.addAttribute("description", "传递的description");
        model.addAttribute("keywords", "传递的keywords");
        return "index"; // 对于html文件可以不写后缀名
    }
    @GetMapping("/UserDemo")
    public String UserDemo(Model model){
        User user = new User();
        user.setUsername("LIHUA");
        user.setUsername("123456");
        user.setSex(1);
        user.setIsVip(true);
        user.setBirthday(new Date());
        user.setHobbys(Arrays.asList("PHP", "Java", "C++"));
        model.addAttribute("user", user);
        return "UserDemo";
    }
}

UserDemo.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<!--    第一种写法-->
    <h2 th:text="${user.username}"></h2>
    <p th:text="${user.password}"></p>
    <p th:if="${user.isVip}">会员</p>
<!--    第二种写法-->
    <div th:object="${user}">
        <h2 th:text="*{username}"></h2>
        <p th:if="*{isVip}">男</p>
    </div>
<!--th:each-->
    <ul>
    <!--注意这tag变量的命名位置-->
        <li th:each="tag:${user.hobbys}" th:text="${tag}"></li>
    </ul>
<!--th:switch-->
    <div th:switch="${user.sex}">
        <p th:case="1">男</p>
        <p th:case="2">女</p>
        <p th:case="*">机器人</p>
    </div>
</html>  

运行结果

通过运行结果可以知道,这些用法的具体效果展示。

thymeleaf中js与css的使用

创建一个css文件

style.css

.box{
    height: 200px;
    width:200px;
    background-color: pink;
}

UserDemo.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<link rel="stylesheet" th:href="@{style.css}">
<div class="box"></div>
<script th:inline="javascript">
    // 后面的{}里面如果没有内容,就会填充注释里面的内容
    const user = /*[[${user}]]*/{};
    console.log(user)
</script>
</html>  

其他效果,让最后一个列表变成红色。

index.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<link rel="stylesheet" th:href="@{style.css}">
<style>
    .active{
        color:red;
    }
</style>
<div class="box"></div>
<!--th:each-->
<ul>
    <li th:each="tag:${user.hobbys}" th:text="${tag}"></li>
</ul>
<ul>
<!--    classappend是追加属性的意思-->
<!--    然后这是演示如何在把最后一个元素编程active-->
    <li th:each="tag, state:${user.hobbys}"
        th:text="${tag}"
        th:classappend="${state.last}?active"
        >
    </li>
</ul>
<script th:inline="javascript">
    // 后面的{}里面如果没有内容,就会填充注释里面的内容
    const user = /*[[${user}]]*/{};
    console.log(user)
</script>
</html>  

thymeleaf组件的使用

第一部分 replace insert id

这里演示了thymeleaf中组件的replace与insert的用法,还提到了另一种方式,就是用id替换fragment。

演示代码

component1.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="com1">
    我是第一个组件
</footer>
<div id="com2">
    我是第二个组件
</div>
</html>    

index.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">默认的title</title>
<!--    字符串拼接-->
    <meta th:content="|李华的-${description}|" name="description" content="默认的description">
    <meta th:content="${keywords}" name="keywords" content="默认的keywords">
    <link rel="stylesheet" th:href="@{style.css}">
</head>
<!--这个是替换效果-->
<!--replace com1-->
<div th:replace="~{components/component1::com1}">
</div>
<!--这个是插入效果-->
<!--insert com1-->
<div th:insert="~{components/component1::com1}"></div>
<!--id形式插入-->
<div th:insert="~{components/component1::#com2}"></div>
</html>  

运行结果

从结果可以看出来,第一个是采取的replace的方式,这个方式之下,是直接用组件的内容,替换原来位子的内容的,然后另一个是insert的方式,在这个方式之下,是在原来的组件的前提之下,内部插入一个组件,然后还有一个id的方式,效果和第一个差不多。

传值
第一种

组件中也可以使用原来文本中数据对象。

代码演示

component1.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="com1">
    我是第一个组件
</footer>
<div id="com2">
    我是第二个组件
</div>
<div th:fragment="com3(message)">
    <p th:text="${message}"></p>
</div>
</html> 

index.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">默认的title</title>
<!--    字符串拼接-->
    <meta th:content="|李华的-${description}|" name="description" content="默认的description">
    <meta th:content="${keywords}" name="keywords" content="默认的keywords">
    <link rel="stylesheet" th:href="@{style.css}">
</head>
<!--这个是替换效果-->
<!--replace com1-->
<div th:replace="~{components/component1::com1}">
</div>
<!--这个是插入效果-->
<!--insert com1-->
<div th:insert="~{components/component1::com1}"></div>
<!--id形式插入-->
<div th:insert="~{components/component1::#com2}"></div>
<div th:insert="~{components/component1::com3('传递过来的数据')}"></div>
</html>  

运行结果

第二种

index.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:text="${title}">默认的title</title>
<!--    字符串拼接-->
    <meta th:content="|李华的-${description}|" name="description" content="默认的description">
    <meta th:content="${keywords}" name="keywords" content="默认的keywords">
    <link rel="stylesheet" th:href="@{style.css}">
</head>
<!--这个是替换效果-->
<!--replace com1-->
<div th:replace="~{components/component1::com1}">
</div>
<!--这个是插入效果-->
<!--insert com1-->
<div th:insert="~{components/component1::com1}"></div>
<!--id形式插入-->
<div th:insert="~{components/component1::#com2}"></div>
<div th:insert="~{components/component1::com3('传递过来的数据')}"></div>
<div th:insert="~{components/component1::com4(~{::#message})}">
    <p id="message">替换模块</p>
</div>
</html>  

component1.html

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="com1">
    我是第一个组件
</footer>
<div id="com2">
    我是第二个组件
</div>
<div th:fragment="com3(message)">
    <p th:text="${message}"></p>
</div>
<div th:fragment="com4(message)">
    <div th:replace="${message}"></div>
</div>
</html>  

运行结果

第三种

这种方式需要的是,写一行注释,不然的话会报错的

<!doctype html>
<html lang="ch" xmlns:th="http://www.thymeleaf.org">
<footer th:fragment="com1">
    <!--/*@thymesVar id="user" type="com.example.thymeleafdemo.Bean.User"*/-->
    <h2 th:text="${user.username}"></h2>
</footer>
<div id="com2">
    我是第二个组件
</div>
<div th:fragment="com3(message)">
    <p th:text="${message}"></p>
</div>
<div th:fragment="com4(message)">
    <div th:replace="${message}"></div>
</div>
</html>


相关文章
|
XML 缓存 前端开发
Thymeleaf一篇就够了
Thymeleaf是Springboot官方支持的模板引擎,有着动静分离等独有特点,通过本文简单学习下吧!
63537 24
Thymeleaf一篇就够了
|
C++ Windows
masscan for windows windows编译masscan
masscan for windows windows编译masscan
791 0
masscan for windows windows编译masscan
|
9月前
|
JSON 前端开发 Java
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
本文介绍了 Thymeleaf 在 Spring Boot 项目中的使用方法,包括访问静态页面、处理对象和 List 数据、常用标签操作等内容。通过示例代码展示了如何配置 404 和 500 错误页面,以及如何在模板中渲染对象属性和列表数据。同时总结了常用的 Thymeleaf 标签,如 `th:value`、`th:if`、`th:each` 等,并提供了官方文档链接以供进一步学习。
729 0
微服务——SpringBoot使用归纳——Spring Boot集成Thymeleaf模板引擎——Thymeleaf 的使用
|
前端开发 JavaScript 索引
Thymeleaf基础语法讲解【详解版】
该内容介绍了Thymeleaf模板引擎的一些基本表达式和语法。主要包括: 1. 变量表达式 `${}` 和 `*{}`,用于获取对象属性,`*{}` 需先通过 `th:object` 指定对象。 2. 链接表达式 `@{}`,用于构建应用路径并引入静态资源,但可能暴露版本号带来安全问题。 3. 迭代循环使用 `th:each`,可获取状态变量如索引、序号、奇偶性等。 4. 条件判断用 `th:if` 和 `th:unless`,基于不同类型的值进行逻辑判断。 示例代码包括了遍历集合、设置表单输入值、条件渲染等场景。
654 0
|
11月前
|
移动开发 前端开发 JavaScript
SpringBoot3 整合Thymeleaf 模板引擎
Thymeleaf 是一个基于 Java 的现代模板引擎,支持 HTML 原型,文件后缀为 .html,可直接在浏览器中查看静态效果。它与 Spring Boot 完美整合,默认配置即可使用,无需额外视图解析器设置。Thymeleaf 支持多种表达式(如变量、链接、国际化等)和 th 属性(如 th:text、th:if 等),适用于 Web 和非 Web 应用开发。通过 th:fragment、th:insert、th:replace 和 th:include 等属性,可以抽取和复用公共页面片段,并支持参数传递。
1644 12
|
存储 Java 测试技术
阿里巴巴java开发手册
这篇文章是关于阿里巴巴Java开发手册的整理,内容包括编程规约、异常日志、单元测试、安全规约、MySQL数据库使用以及工程结构等方面的详细规范和建议,旨在帮助开发者编写更加规范、高效和安全的代码。
|
Java Spring
Spring boot +Thymeleaf 本地图片加载失败(图片路径)的问题及解决方法
这篇文章详细讲解了在Spring Boot应用程序中本地图片无法加载的问题原因,并提供了两个示例来说明如何通过使用正确的相对路径或Thymeleaf语法来解决图片路径问题。
|
数据采集 机器学习/深度学习 算法
深入Sklearn预处理技术:数据清洗与标准化实战
【7月更文第22天】在机器学习项目中,数据预处理是至关重要的一步,它直接影响到模型的性能和准确性。Scikit-learn(简称sklearn)作为Python中最受欢迎的机器学习库之一,提供了丰富的数据预处理工具。本文将深入探讨sklearn中的数据清洗与标准化技术,并通过实战代码示例展示如何应用这些技术提升模型效果。
1375 2
|
存储 NoSQL MongoDB
MongoDB实战面试指南:常见问题一网打尽
MongoDB实战面试指南:常见问题一网打尽
|
测试技术 Shell 开发工具
Playwright 系列(13):如何调试测试用例
Playwright 系列(13):如何调试测试用例
874 0
Playwright 系列(13):如何调试测试用例