提示:以下是本篇文章正文内容,下面案例可供参考
Thymeleaf 是一个跟 FreeMarker 类似的模板引擎,它可以完全替代 JSP 。;
动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,无网络显示静态内容,有网络用后台得到数据替换静态内容;
与SpringBoot完美整合,springboot默认整合thymelea
Thymeleaf中的表达式分为三种
就是OGNL表达式或者Spring EL表达式(spring中在jsp页面中获取session或其他内置对象的数据)如:${session.user.name};
它们将以HTML标签的一个属性来表示:
<h5>表达式h5>
<span>${text}span>
<span th:text="${text}">你好 thymleafspan>
也可以叫星号表达式,有点像变量表达式,不过它们用一个预先选择的对象来代替上下文变量容器(map)来执行,如: *{customer.name};被指定的对象object由th:object属性定义,
代码如下:
<tr th:each="user, status : ${users}" th:object="${user}" th:bgcolor="${status.even}? 'gray'">
<td th:text="${status.even}">truetd>
<td th:text="${status.odd}">truetd>
<td th:text="${status.index}">0td>
<td th:text="${status.count}">1td>
<td th:text="${user.id}">1td>
<td th:text="*{name}">zhangsantd>
<td th:text="*{userName}">张三td>
<td th:text="${user.age}">20td>
<td th:text="${user.sex} == 1 ? '男' : '女'">男td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30td>
<td th:text="${user.note}">1td>
<td>
<a th:href="@{delete(id=${user.id}, userName= *{userName})}">删除a>
<a th:href="|/update/${user.id}|">修改a>
<a th:href="'approve/' + ${user.id}">审核a>
td>
tr>
<a th:href="@{/delete(id=${user.id}, userName=*{userName})}">删除a>
<a th:href="|/update/${user.id}|">修改
<a th:href="'/approve/' + ${user.id}">审核a>
字面(Literals)
<a th:href="|/update/${user.id}|">修改a>
<a th:href="'/approve/' + ${user.id}">审核a>
if是条件成立是生效,unless是条件不成立是生效,两者恰好相反
<h5>if指令h5>
<a th:if="${users.size() > 0}">查询结果存在a><br>
<a th:if="${users.size() <= 0}">查询结果不存在a><br>
<a th:unless="${session.user != null}" href="#">登录a><br>
<tr th:each="user, status : ${users}" th:object="${user}" th:bgcolor="${status.even}? 'gray'">
<td th:text="${status.even}">truetd>
<td th:text="${status.odd}">truetd>
<td th:text="${status.index}">0td>
<td th:text="${status.count}">1td>
<td th:text="${user.id}">1td>
<td th:text="*{name}">zhangsantd>
<td th:text="*{userName}">张三td>
<td th:text="${user.age}">20td>
<td th:text="${user.sex} == 1 ? '男' : '女'">男td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30td>
<td th:text="${user.note}">1td>
<td>
<a th:href="@{delete(id=${user.id}, userName= *{userName})}">删除a>
<a th:href="|/update/${user.id}|">修改a>
<a th:href="'approve/' + ${user.id}">审核a>
td>
tr>
以**[[…]]**方式表示,使用时必须使用“th:inline=”text”激活,th:inline可以在
父级标签内使用,甚至作为body的标签。内联文本尽管比th:text的代码少,不利于原型显示。使用的内容必须是后端返回的model总有的内容;
<h6 th:text="${text}">静态内容h6>
<h5>内联文本h5>
<div>
<h6 th:inline="text">[[${text}]]h6>
<h6 th:inline="none">[[${text}]]h6>
<h6>[[${text}]]h6>
div>
**PS:**能用th指令就用替换指令
与内联文本相似,以th:inline=”text/javascript/none”激活,内容是js脚本。拥有js脚本的能力;
<h5>内联jsh5>
<script th:inline="javascript">
var text = '[[${text}]]';
console.log(text);
script>
Thymeleaf提供了了一系列Utility对象(内置于Context中),可以通过**# + 对象名**的方式直接调用其中的方法。
对象 | 功能 |
---|---|
dates | java.util.Date**的功能方法类。 |
calendars | 类似#dates,面向java.util.Calendar |
numbsers | 格式化数字的功能方法类 |
strings | 字符串对象的功能类 |
objects | 对objects的功能类操作。 |
bools | 对布尔值求值的功能方法。 |
arrays | 对数组的功能类方法。 |
lists/sets/maps/ | 对list/set/map功能类方法 |
通过footer标签定义页面;
DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<footer th:fragment="copy(title)">
© 2020 开课吧版权所有<br>
<span th:text="${title}">title footerspan>
footer>
body>
html>
然后,在页面任何地方引入:
<h5>thymeleaf布局h5>
<div th:insert="footer :: copy('开课吧1')">div>
<div th:replace="footer :: copy('开课吧2')">div>
<div th:include="footer :: copy('开课吧3')">div>
Thymelea启动器在依赖坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.kaikebagroupId>
<artifactId>sb-thymeleaf-01artifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.3.11.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>2.1.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.25version>
dependency>
dependencies>
project>
application.properties
server.port=8080
logging.level.com.kaikeba=debug
# 数据库信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot-jdbc?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.password=root
spring.datasource.username=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 关闭thymeleaf缓存
spring.thymeleaf.cache=false
由于springboot为thymeleaf自动添加的视图解析器会加上templates前缀,所以我们的所有静态文件都必须在**/resources/templates/** 下,
定义的布局footer.html:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<footer th:fragment="copy(title)">
©2020开课吧版权所有<br/>
<span th:text="${title}">title footerspan>
footer>
body>
html>
测试的temp.html:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h2>Thymeleaf布局h2>
<div th:insert="footer :: copy('开课吧1')">div>
<div th:replace="footer :: copy('开课吧22')">div>
<div th:include="footer :: copy('开课吧333')">div>
<h2>内置变量h2>
<span th:text="${#dates.format(#dates.createNow(), 'yyyy-MM-dd HH:mm:ss')}">获取当前日期span><br/>
<span th:text="${#strings.substring(text, 4, 6)}">截取字符串span><br/>
<span th:text="${#strings.length(text)}">span><br/>
<span th:text="${#strings.randomAlphanumeric(6)}">随机字符串span><br/>
<span th:text="${#strings.equals(text, 'hello')}">equals等值判断span><br/>
<h2>if指令h2>
<a th:if="${users.size() > 0}"> 查询结果存在a><br/>
<a th:if="${users.size() <= 0}">查询结果不存在a><br/>
<a th:unless="${session.user != null}">登录a><br/>
<h2>内联文本h2>
<span th:text="${text}">静态内容span>
<div>
<h3 th:inline="text">${text}h3>
<h3>[[${text}]]h3>
div>
<h3>内联JSh3>
<span>内容在console.logspan>
<script th:inline="javascript">
var text = '[[${text}]]';
console.log(text);
script>
body>
html>
测试循环的user.html:
DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf 入门案例title>
<meta charset="UTF-8">
<style type="text/css">
table {
border-collapse: collapse;
font-size: 15px;
width: 80%;
margin: auto;
}
table, th, td {
border: 1px solid darkslategray;
padding: 10px
}
style>
head>
<body>
<div style="text-align: center">
<span style="color: darkslategray; font-size: 30px">欢迎光临span>
<hr/>
<table class="list">
<tr>
<th>偶数行th>
<th>奇数行th>
<th>序号indexth>
<th>序号countth>
<th>IDth>
<th>姓名th>
<th>用户名th>
<th>年龄th>
<th>性别th>
<th>生日th>
<th>备注th>
<th>操作th>
tr>
<tr th:each="user, status : ${users}" th:object="${user}" th:bgcolor="${status.even}? 'gray'">
<td th:text="${status.even}">truetd>
<td th:text="${status.odd}">truetd>
<td th:text="${status.index}">0td>
<td th:text="${status.count}">1td>
<td th:text="${user.id}">1td>
<td th:text="*{name}">zhangsantd>
<td th:text="*{userName}">张三td>
<td th:text="${user.age}">20td>
<td th:text="${user.sex} == 1 ? '男' : '女'">男td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd')}">1980-02-30td>
<td th:text="${user.note}">1td>
<td>
<a th:href="@{delete(id=${user.id}, userName= *{userName})}">删除a>
<a th:href="|/update/${user.id}|">修改a>
<a th:href="'approve/' + ${user.id}">审核a>
td>
tr>
table>
div>
body>
html>
package com.kaikeba.controller;
import com.kaikeba.entity.pojo.User;
import com.kaikeba.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import java.util.List;
/**
* @author sunyang
* @date 2021/5/31 11:32
*/
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/all")
public String searchAll(Model model) {
List<User> all = userService.findAll();
model.addAttribute("users", all);
model.addAttribute("text","我这是测试th:text");
return "user";
}
/**
* @desc thymeleaf测试
*
* @param model
* @return java.lang.String
* @date 2021/5/31 16:42
* @auther sunyang
*/
@GetMapping("/temp")
public String temp(Model model) {
List<User> all = userService.findAll();
model.addAttribute("users", all);
model.addAttribute("text","我这是测试th:text");
return "temp";
}
}
package com.kaikeba.entity.pojo;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
/**
* @author sunyang
* @date 2021/5/31 11:30
*/
@Table(name = "tbs_user")
public class User {
// 主键 自增
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private Date created;
private Date updated;
private String note;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
this.updated = updated;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birthday=" + birthday +
", created=" + created +
", updated=" + updated +
", note='" + note + '\'' +
'}';
}
}
package com.kaikeba.mapper;
import com.kaikeba.entity.pojo.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @author sunyang
* @date 2021/5/31 11:29
*/
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
}
package com.kaikeba.service;
import com.kaikeba.entity.pojo.User;
import java.util.List;
/**
* @author sunyang
* @date 2021/5/31 11:29
*/
public interface UserService {
/**
* @return java.util.List
* @desc 查询所有数据
* @date 2021/5/31 11:31
* @auther sunyang
*/
List<User> findAll();
}
package com.kaikeba.service.impl;
import com.kaikeba.entity.pojo.User;
import com.kaikeba.mapper.UserMapper;
import com.kaikeba.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* @author sunyang
* @date 2021/5/31 11:30
*/
@Service
public class UserServiceImpl implements UserService {
@Resource
private UserMapper userMapper;
@Override
public List<User> findAll() {
return userMapper.selectAll();
}
}