SpringBoot中使用Thymeleaf模板

简介: SpringBoot中使用Thymeleaf模板

一.什么是Thymeleaf

官网原话:https://www.thymeleaf.org/
  Thymeleaf是适用于Web和独立环境的现代服务器端Java模板引擎,能够处理HTML,XML,JavaScript,CSS甚至纯文本。
  Thymeleaf的主要目标是提供一种优雅且高度可维护的模板创建方式。为此,它以自然模板的概念为基础,以不影响模板用作设计原型的方式将其逻辑注入模板文件。这样可以改善设计沟通,并缩小设计团队与开发团队之间的差距。
  Thymeleaf是一个HTML5模板引擎,可用于Web环境中的应用开发。Thymeleaf提供了一个用于整合Spring MVC的可选模块,在应用开发中,你可以使用Thymeleaf来完全代替JSP或其他模板引擎,如Velocity、FreeMarker等。Thymeleaf的主要目标在于提供一种可被浏览器正确显示的、格式良好的模板创建方式。
  thymeleaf模板引擎,替代jsp。

二.SpringBoot中使用Thymeleaf模板

1.pom.xml中添加thymeleaf依赖

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

2.关闭thymeleaf缓存

在application.yml中的spring:下添加如下代码(能让改动的页面及时生效,实现类似热部署效果):

#能让改动的页面及时生效,实现类似热部署效果
thymeleaf:
    cache: false

注意缩进,添加后缩进如下:
在这里插入图片描述

3.创建thymeleaf模板页面

创建一个普通的html文件hello.html,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
</html>

在html的标签上加入名称空间xmlns:th="http://www.thymeleaf.org"表示该页面是一个thymeleaf模板页面。
即把上述代码中<html lang="en">换成<html lang="en" xmlns:th="http://www.thymeleaf.org">
这样就可以在页面中的标签内使用th属性取出model中的值,类似于EL表达式。
具体用法代码如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="'欢迎来到中国,我叫'+${name}+',今年'+${age}+'岁。'"></p>
    <p>欢迎来到中国,我叫<span th:text="${name}"></span>,今年<span th:text="${age}"></span>岁。</p>
</body>
</html>

4.创建一个类(用于与上述html页面交互)

ackage com.ysw.springboot01.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/thy")
public class ThymeleafController {
        

    @RequestMapping("/hello")
    public String hello0(Model model){
        
        //向model中存入数据
        model.addAttribute("name","李白");
        model.addAttribute("age","18");
        //跳转到hello.html模版引擎
        return "hello";
    }
}


5.访问服务路径,效果如下:

在这里插入图片描述

目录
相关文章
|
5月前
|
前端开发 Java
SpringBoot下载xlsx模板,导出excel数据
SpringBoot下载xlsx模板,导出excel数据
374 0
|
Java API Spring
Java SpringBoot 公众号集成模板推送消息
Java SpringBoot 公众号集成模板推送消息
|
Java Maven
springboot项目--freemarker使用ftl模板文件动态生成图片
springboot项目--freemarker使用ftl模板文件动态生成图片
611 0
|
5月前
|
XML JavaScript 前端开发
springboot配合Freemark模板生成word,前台vue接收并下载【步骤详解并奉上源码】
springboot配合Freemark模板生成word,前台vue接收并下载【步骤详解并奉上源码】
|
前端开发 Java
SpringBoot-6-模板Thymeleaf常用标签
SpringBoot-6-模板Thymeleaf常用标签
79 0
SpringBoot-6-模板Thymeleaf常用标签
|
存储 JavaScript 前端开发
【开发模板】Vue和SpringBoot的前后端分离开发模板(二)
【开发模板】Vue和SpringBoot的前后端分离开发模板
|
5月前
|
算法 Java
制作SpringBoot工程模板
制作SpringBoot工程模板
55 0
|
5月前
|
移动开发 运维 前端开发
SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
99 0
|
5月前
|
Oracle Java 关系型数据库
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
87 0
|
10月前
|
Java
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
application.properties模板+application.yml模板+pom模板+mapper.xml模板(springboot)
70 0