目录
Thymeleaf 是一个流行的模板引擎,该模板引擎采用 Java 语言开发。
模板引擎是一个技术名词,是跨领域跨平台的概念,在 Java 语言体系下有模板引擎,在 C#、PHP 语言体系下也有模板引擎,甚至在 JavaScript 中也会用到模板引擎技术,Java 生态下 的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(国产) 等。
Thymeleaf 对网络环境不存在严格的要求,既能用于 Web 环境下,也能用于非 Web 环境 下。在非 Web 环境下,他能直接显示模板上的静态数据;在 Web 环境下,它能像 Jsp 一样从 后台接收数据并替换掉模板上的静态数据。它是基于 HTML 的,以 HTML 标签为载体, Thymeleaf 要寄托在 HTML 标签下实现。
Spring Boot 集成了 Thymeleaf 模板技术,并且 Spring Boot 官方也推荐使用 Thymeleaf 来 替代 JSP 技术,Thymeleaf 是另外的一种模板技术,它本身并不属于 Spring Boot,Spring Boot 只是很好地集成这种模板技术,作为前端页面的数据展示,在过去的 Java Web 开发中,我们 往往会选择使用 Jsp 去完成页面的动态渲染,但是 jsp 需要翻译编译运行,效率低 。
Thymeleaf 的官方网站:Thymeleaf
Thymeleaf 官方手册:Tutorial: Using Thymeleaf
①创建项目,引入web和thymeleaf依赖
②pom.xml主要依赖
- "1.0" encoding="UTF-8"?>
- <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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.15version>
- <relativePath/>
- parent>
- <groupId>com.zlgroupId>
- <artifactId>springboot-myredisartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>springboot-myredisname>
- <description>springboot-myredisdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-thymeleafartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-devtoolsartifactId>
- <scope>runtimescope>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
- project>
③Controller编写存放数据
- package com.zl.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
-
- import javax.servlet.http.HttpServletRequest;
-
- @Controller
- public class HelloThymeleafController {
- @GetMapping("/hello")
- public String helloThymeleafController(HttpServletRequest request){
- // 添加数据到request域当中,模板引擎可以从request域当中获取
- request.setAttribute("data","第一个Thymeleaf数据");
- // 返回到视图(html)
- return "hello";
- }
- }
④视图hello.html取出数据(放到templates中)
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <h1 th:text="${data}">h1>想显示的数据
- body>
- html>
⑤进行访问
模板引擎的常用配置
思考:为什么controller返回一个字符串就可以直接找到templates下的对应html呢?这些和模板引擎的默认设置是有关的!
application.properties配置
注:在开发阶段只需要把cache设置为false即可,其它都是默认选项不需要修改!
- #开发阶段设置为 false ,上线后设置为 true,默认是true走缓存
- spring.thymeleaf.cache=false
- # 设置模版文件的路径,前缀,默认就是classpath:/templates/
- spring.thymeleaf.prefix=classpath:/templates/
- # 设置后缀,默认就是 .html
- spring.thymeleaf.suffix=.html
- #编码方式,默认就是UTF-8,所以中文不会乱码
- spring.thymeleaf.encoding=UTF-8
- #模板的类型,默认是HTML,模板是html文件
- spring.thymeleaf.mode=HTML
表达式是在页面获取数据的一种 thymeleaf 语法。类似 ${key} 。
注意:th:text="${key}" 是 Thymeleaf 的一个属性,用于文本的显示。
语法: ${key} 。
说明:标准变量表达式用于访问容器(tomcat)上下文环境中的变量,功能和 EL 中的 ${} 相 同。Thymeleaf 中的变量表达式使用 ${变量名} 的方式获取 Controller 中 model 其中的数据。 也就是 request 作用域中的数据。
准备Student数据
- package com.zl.pojo;
-
- public class Student {
- private String name;
- private Integer age;
-
- public Student(String name, Integer age) {
- this.name = name;
- this.age = age;
- }
-
- public Student() {
- }
-
- @Override
- public String toString() {
- return "Student{" +
- "name='" + name + '\'' +
- ", age=" + age +
- '}';
- }
-
- 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;
- }
- }
欢迎页面index.html放入静态资源static下,不走模板引擎
- html>
- <html lang="en" >
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <a href="thymeleaf/expression1">标准变量表达式a>
- body>
- html>
application.properties
- #配置端口号
- server.port=8082
- #开发阶段设置为 false ,上线后设置为 true,默认是true走缓存
- spring.thymeleaf.cache=false
controller存放数据,跳转到expression1
- package com.zl.controller;
-
- import com.zl.pojo.Student;
- import org.springframework.stereotype.Controller;
- import org.springframework.ui.Model;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/thymeleaf")
- public class ThymeleafExpressionController {
- @GetMapping("/expression1")
- public String expression1(Model model){
- // 添加数据
- model.addAttribute("student",new Student("Jack",18));
- // 跳转到视图
- return "expression1";
- }
- }
expression1.html接收数据
注:${student.getName()}也能获取到值,底层实际上就是调用对应的Get方法!
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>expression1title>
- head>
- <body>
- <h1 th:text="${student.name}">h1>
- <h1 th:text="${student.age}">h1>
- <h1 th:text="${student.getAge()}">h1>
- body>
- html>
执行结果:
语法:*{key} 。
说明:不能单独使用,需要配和 th:object 一起使用。选择变量表达式,也叫星号变量表达式,使用 th:object 属性来绑定对象,选择表达式首先使用 th:object 来绑定后台传来的对象,然后使用 * 来代表这个对象,后面 {} 中的值是此对象中的属性。可以简化上述的开发。
欢迎页面
- html>
- <html lang="en" >
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <a href="thymeleaf/expression1">标准变量表达式a>
- <a href="thymeleaf/expression2">选择变量表达式a>
- body>
- html>
controller存放数据
- @GetMapping("/expression2")
- public String expression2(Model model){
- model.addAttribute("student",new Student("Jack",18));
- return "expression2";
- }
expression2.html取出数据:使用th:object +*{key}简化上述的开发
- html>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>expression2title>
- head>
- <body>
-
- <div th:object="${student}">
-
- <p th:text="*{name}">p>
- <p th:text="*{age}">p>
- div>
-
- <p th:text="*{student.name}">p>
- body>
- html>
语法:@{链接 url} 。
说明:主要用于链接、地址的展示,可用于