当前位置: 首页 > news >正文

thymeleaf

一、理论

thymeleaf是Java模板引擎,模板是指我们开发好的前端页面,其中页面里涉及到的数据只需要调用后端系统查询即可,所以前端页面是不变的,变的是从后端返回的数据。

二、实践

依赖

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

配置文件

spring:thymeleaf:cache: falseencoding: utf-8

th:text

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("name", "zhangsan");return "index";}
}

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<div th:text="|你的名字:${name}|">静态名字</div>
</body>
</html>

th:each

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {List<Student> studentList = new ArrayList<>();studentList.add(new Student("zhangsan", 14));studentList.add(new Student("ls", 28));request.setAttribute("studentList", studentList);Map<String, Integer> infoMap = new HashMap<>();infoMap.put("zhaoliu", 17);infoMap.put("chenqi", 18);request.setAttribute("infoMap", infoMap);return "index";}
}@Data
@AllArgsConstructor
class Student {private String name;private int age;
}

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<div th:each="student,studentStat:${studentList}"><p th:text="${studentStat.index}"></p><p th:text="${student.name}"></p><p th:text="${student.age}"></p>
</div>
<div th:each="info,infoStat:${infoMap}"><p th:text="${infoStat.index}"></p><p th:text="${info.key}"></p><p th:text="${info.value}"></p>
</div>
</body>
</html>

th:if

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("result1", 1 > 2);request.setAttribute("result2", 1 < 2);return "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:if="${result1}">1大于2</p>
<p th:if="${result2}">1小于2</p>
</body>
</html>

th:href

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("name", "zhangsan");request.setAttribute("age", 14);return "index";}@GetMapping("/a/b")public String ab(@RequestParam("name") String name,@RequestParam("age") Integer age) {System.out.println("后端收到值:name=" + name + ",age=" + age); // 后端收到值:name=zhangsan,age=14return "index";}@GetMapping("/c/d")public String cd(Student student) {System.out.println("后端收到值:" + student); // 后端收到值:Student(name=zhangsan, age=14)return "index";}
}@Data
@ToString
class Student {private String name;private Integer age; // int 会报错
}

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<a th:href="@{/a/b(name=${name},age=${age})}">点击</a>
<a th:href="@{/c/d(name=${name},age=${age})}">点击</a>
</body>
</html>

th:switch

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("sex", "male");return "index";}
}

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<div th:switch="${sex}"><p th:case="male">male</p><p th:case="female">female</p><p th:case="*">其他</p>
</div>
</body>
</html>

内联文本

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("name", "zhangsan");return "index";}
}

html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<div th:inline="text"><h3>我的姓名是:[[${name}]]</h3>
</div>
</body>
</html>

自定义模板

controller:

@Controller
public class MicoZoneController {@GetMapping("/index")public String index(HttpServletRequest request) {request.setAttribute("name", "zhangsan");return "index";}
}

header.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>header</title>
</head>
<body>
<div th:fragment="top"><h1>我是top模板</h1>
</div>
</body>
</html>

foot.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>foot</title>
</head>
<body>
<div th:fragment="bottom"><h1>我是bottom模板</h1>
</div>
</body>
</html>

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>index</title>
</head>
<body>
<div th:insert="fragment/header :: top"></div>
<div th:text="${name}"></div>
<div th:insert="fragment/foot :: bottom"></div>
</body>
</html>

http://www.mrgr.cn/news/48239.html

相关文章:

  • 条款34 考虑lambda而非std::bind
  • [SZ901] JTAG合并功能(类似FPGA菊花链)
  • 深入理解Kafka:核心设计与实践原理读书笔记
  • 机器人路径规划和避障算法matlab仿真,分别对比贪婪搜索,最安全距离,RPM以及RRT四种算法
  • 抓包 127.0.0.1 (loopback) 使用 tcpdump+wireshark
  • Qt5HttpServer : Qt官方的HTTP服务器
  • docker 安装与使用
  • 【cocos creator】输入框滑动条联动小组建
  • OpenRewrite配方之代码格式化——org.openrewrite.java.format.AutoFormat
  • 毕业设计之—基于ManTra-Net的图像篡改检测方法研究与应用实现
  • 你的拼命向前,只不过是别人的轻松实现
  • 【D3.js in Action 3 精译_033】4.1.0 DIY 实战:如何通过学习 d3.autoType 函数深度参与 D3 生态建设
  • Antsword-labs靶机渗透
  • QT:数据库,opencv
  • MySQL-02.概述-安装配置
  • 共识算法Raft
  • std::future::then的概念和使用方法
  • 让UE通过EPC连接到互联网
  • 基于JAVA+SpringBoot+Vue的医疗报销系统
  • 微积分复习笔记 Calculus Volume 1 - 2.3 The Limit Laws
  • 上传图片到github上,生成链接在Typora中使用(解决Typora的md文件在分享时的丢失问题)
  • 死磕P7:JVM性能调优必知必会(二)
  • 付费计量系统实体和接口(7)
  • [C语言] 函数详解:库函数与自定义函数
  • 职场上的人情世故你知多少
  • 从automaxprocs库浅窥Linux容器的资源控制