(二)Springboot 整合thymeleaf

本文介绍了Thymeleaf模板引擎的基本概念,包括其在有网和无网环境下的运行特性,以及如何在Spring Boot项目中集成Thymeleaf,实现页面的热更新和动态数据绑定。文章还提供了配置示例和代码片段,帮助读者快速上手。

一、Thymeleaf 是个什么?

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

    1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

    2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

    3. Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

话不多说直接上代码

(1)添加依赖

<!-- thymeleaf 模板引擎 start-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
   <groupId>net.sourceforge.nekohtml</groupId>
   <artifactId>nekohtml</artifactId>
   <version>1.9.22</version>
</dependency>
<!-- thymeleaf 模板引擎 end-->

说明:使用springboot的thymeleaf模板时默认会对HTML进行严格的检查,导致当你的标签没有闭合时就会通不过。大家写html的时候,一定要注意标签闭合,nekohtml这个依赖可以解决这一问题。

(1)properties添加配置,

#thymeleaf
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5

这两个就够了,说明一下:

第一行配置是清除缓存,实现热部署。也就是修改了html后不用重启,刷新页面就能看到效果。不过这儿特别强调一下,修改完html后一定要ctrl+f9重新build一下。再回到浏览器刷新,就能看到效果了。

第二行配置是回避HTML进行严格的检查的配置,当然你需要提前引入nekohtml依赖。

当然还有其他很多配置例如:

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html

thymeleaf的配置文件说明 

#spring.thymeleaf.cache = true #启用模板缓存。 

#spring.thymeleaf.check-template = true #在呈现模板之前检查模板是否存在。

#spring.thymeleaf.check-template-location = true #检查模板位置是否存在。

#spring.thymeleaf.content-type = text / html #Content-Type值。 

#spring.thymeleaf.enabled = true #启用MVC Thymeleaf视图分辨率。 

#spring.thymeleaf.encoding = UTF-8 #模板编码。 

#spring.thymeleaf.excluded-view-names = #应该从解决方案中排除的视图名称的逗号分隔列表。 

#spring.thymeleaf.mode = HTML5 #应用于模板的模板模式。另请参见StandardTemplateModeHandlers。 

#spring.thymeleaf.prefix = classpath:/ templates / #在构建URL时预先查看名称的前缀。 

#spring.thymeleaf.suffix = .html #构建URL时附加到查看名称的后缀。 

#spring.thymeleaf.template-resolver-order = #链中模板解析器的顺序。 

#spring.thymeleaf.view-names = #可以解析的视图名称的逗号分隔列表。/ templates / #在构建URL时先查看名称的前缀。 

#spring.thymeleaf.suffix = .html #构建URL时附加到查看名称的后缀。 

#spring.thymeleaf.template-resolver-order = #链中模板解析器的顺序。 

#spring.thymeleaf.view-names = #可以解析的视图名称的逗号分隔列表。/ templates / #在构建URL时先查看名称的前缀。 

#spring.thymeleaf.suffix = .html #构建URL时附加到查看名称的后缀。 

#spring.thymeleaf.template-resolver-order = #链中模板解析器的顺序。 

#spring.thymeleaf.view-names = #可以解析的视图名称的逗号分隔列表。

控制层代码

@Controller
public class testController {

    @RequestMapping("/test")
    public String test(){
        return "/test";
    }
}

在文件夹template下新建html5文件

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf</title>
</head>
<body>
 <h1>hello thymeleaf</h1>
</body>
</html>

运行结果:

 

完美!!

下一篇将介绍下thymeleaf语法。。。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值