Web开发:Thymeleaf模板引擎

文章目录

Thymeleaf简介

模板引擎

模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。

模板引擎的实现方式有很多,最简单的是"置换型"模板引擎,这类模板引擎只是将指定模板内容(字符串)中的特定标记(子字符串)替换,便生成了最终需要的业务数据(如网页)。"置换型"模板引擎实现简单,但其效率低下,无法满足高负载的应用需求(比如有海量访问的网站),因此还出现了"解释型"模板引擎和"编译型"模板引擎等。

Thymeleaf介绍

Thymeleaf 是面向 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理HTML、XML、JavaScript、CSS 甚至纯文本。Thymeleaf 旨在提供一个优雅的、高度可维护的创建模板的方式。为了实现这一目标Thymeleaf 建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板设计原型,从而改善了设计的沟通,弥合了设计和开发团队之间的差距。Thymeleaf 从设计之初就遵循 Web 标准------特别是 HTML5 标准,如果需要,Thymeleaf允许创建完全符合HTML5验证标准的模板。

Spring Boot 体系内推荐使用 Thymeleaf作为前端页面模板,并且 Spring Boot 2.0中默认使用Thymeleaf3.0,性能提升幅度很大。

Thymeleaf特点

简单说,Thymeleaf是一个跟 Velocity、FreeMarker类似的模板引擎,它可以完全替代JSP与其他的模板引擎相比较,它有如下三个极吸引人的特点。

Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持HTML原型,然后在 HTML标签里增加额外的属性来达到模板+ 数据的展示方式。浏览器解释HTML时会忽略未定义的标签属性,所以Thymeleaf的模板可以静态地运行当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。Thymeleaf开箱即用的特性。它支持标准方言和Spring方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、改JSTL、改标签的困扰。

核心特性

非侵入式模板设计

Thymeleaf允许开发者直接使用标准的HTML代码,并通过添加Thymeleaf特有的属性和表达式(如th:text、th:if等)来动态插入或修改内容。这种设计保持了HTML的清晰和可读性,降低了模板的复杂度,并使得前端设计师和开发者能够更容易地协作。

丰富的功能

hymeleaf提供了丰富的功能,包括数据绑定、条件语句、循环、国际化支持、模板布局等。这些功能使得开发者可以轻松地构建出动态、响应式且易于维护的Web界面。

高性能

Thymeleaf在服务器端执行时,会首先将模板转换为一种优化的执行结构,这种结构使得模板的渲染过程非常高效。它能够在短时间内处理并渲染大量的模板数据,从而满足高并发Web应用的需求。

与Spring Framework无缝集成

Thymeleaf与Spring MVC等Java Web框架集成得非常好,是Spring Boot推荐的默认模板引擎之一。它支持Spring的表达式语言(SpEL),使得数据绑定、表单验证、国际化等功能变得更加简便。

扩展性和可定制性

Thymeleaf提供了良好的扩展性和可定制性,允许开发者根据需要进行配置和优化。开发者可以扩展和创建自定义的方言,以适应不同的开发需求。

使用场景

Thymeleaf适用于各种Web开发场景,无论你是正在构建一个简单的博客网站,还是一个复杂的企业级应用,Thymeleaf都能提供强大的支持。它特别适用于需要高效渲染大量数据和复杂页面的应用,如电商网站、管理系统等。

快速上手

1.创建项目

可以使用Maven工具来创建项目,并添加Thymeleaf的依赖。

xml 复制代码
<dependencies>
        <dependency>
            <groupId>nz.net.ultraq.web.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-spring-boot3-starter</artifactId>
            <version>3.5.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.配置application.yml文件

yml 复制代码
server:
  port: 8080
spring:
  #数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/crm?serverTimezone=GMT-8
    username: root
    password: 123456
  jpa:
    open-in-view: false
#  thymeleaf:
#    cache: true
#    check-template: true
#    check-template-location: true
#    servlet:
#      content-type: text/html
#    encoding: UTF-8
#    excluded-view-names:
#    mode: HTML
#    prefix: /templates/
#    suffix: .html
#    template-resolver-order:
#    view-names:
  #热部署配置
  devtools:
    restart:
      enabled: true
      additional-paths: scr/main/java
      exclude: static/**,public/**
  #配置JPA(Hibernate)相关信息
# 配置日志输出SQ语句
logging:
  level:
    root: warn
    com:
      bdqn:
        mybatisplusjsp:
          mapper: debug
  pattern:
    console: "%p%m%n"```
相关推荐
Q186000000001 分钟前
在HTML中添加视频
前端·html·音视频
bin915318 分钟前
前端JavaScript导出excel,并用excel分析数据,使用SheetJS导出excel
前端·javascript·excel
Rattenking26 分钟前
node - npm常用命令和package.json说明
前端·npm·json
Easonmax26 分钟前
【HTML5】html5开篇基础(1)
前端·html·html5
For. tomorrow30 分钟前
Vue3中el-table组件实现分页,多选以及回显
前端·vue.js·elementui
布瑞泽的童话1 小时前
无需切换平台?TuneFree如何搜罗所有你爱的音乐
前端·vue.js·后端·开源
白鹭凡1 小时前
react 甘特图之旅
前端·react.js·甘特图
打野赵怀真1 小时前
你有看过vue的nextTick源码吗?
前端·javascript
2401_862886781 小时前
蓝禾,汤臣倍健,三七互娱,得物,顺丰,快手,游卡,oppo,康冠科技,途游游戏,埃科光电25秋招内推
前端·c++·python·算法·游戏