SpringBoot-Thymeleaf模板引擎整合及基本用法总结

本文介绍了SpringBoot不推荐使用JSP的原因,探讨了Thymeleaf作为模板引擎的优点和缺点,并展示了如何在SpringBoot中引入和使用Thymeleaf,包括模板页面的存放位置、入门程序、Thymeleaf的特点和一些常用标签的使用。
摘要由CSDN通过智能技术生成

SpringBoot-Thymeleaf模板引擎整合及基本用法总结

(一) 模板引擎引入

(1) 开发方式
在往常的开发中,一旦涉及到一个完整的前后端项目,有两种办法:

一种就是前后端分离,也就是说,约定好接口,通过异步方式,以 Json 字符串作为内容传递,后台进行一些业务逻辑,前台负责界面,以及通过 js 等进行数据的处理以及页面的美化。

还有一种方式就是模板引擎的方式,这种方式也没什么太新奇的,你可以简单的理解为 JSP 的那种模式

现在来说,前后端分离开始更加流行,但是很多旧的项目,或者自己一个人写东西,我感觉使用模板引擎也是非常不错的选择,还有时候去找一些后台的开源模板,有一些也都用了Thymeleaf, 何况出于学习的态度,学哪种技术都是可以的
针对第二种方式继续说一下,JSP 大家应该是熟悉的,比如前端传来一个 html 的页面,我们就会去将这个页面改写为 JSP 页面,我们可以用 JSP 比较容易的实现数据的显示,那么为什么不继续用 JSP 而要用别的模板引擎呢?
注:Thymeleaf 和 Freemarker 等各有特点,用熟悉后,可能会对另一种的使用方式感觉很别扭,没必要争论哪种更好,自己喜欢就行
(2) 为什么用模板引擎
以 Springboot 来说,官方就不推荐使用 JSP ,来看一下官方的解释
Springboot 2.2.7
地址:https://docs.spring.io/spring-boot/docs/2.2.7.RELEASE/reference/html/spring-boot-features.html#boot-features-jsp-limitations
Springboot:7.1.10. Template Engines
As well as REST web services, you can also use Spring MVC to serve dynamic HTML content. Spring MVC supports a variety of templating technologies, including Thymeleaf, FreeMarker, and JSPs. Also, many other templating engines include their own Spring MVC integrations.
Spring Boot includes auto-configuration support for the following templating engines:
FreeMarker、Groovy、Thymeleaf、Mustache

If possible, JSPs should be avoided. There are several known limitations when using them with embedded servlet containers.

上面一段话,总的来说,就是告诉我们,如果我们想要动态的展示HTML内容,就可以用一些模板的技术,例如 FreeMarker、Groovy、Thymeleaf、Mustache
最后有一句话说道点子上了,Springboot 建议我们尽可能的规避 JSP,并且提供了一些说法
Springboot:7.4.5. JSP Limitations
When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.

Undertow does not support JSPs.

Creating a custom error.jsp page does not override the default view for error handling. Custom error pages should be used instead.

第二点是说,Undertow 支持 JSP,而第三点,则是关于error错误页面的覆盖问题
前两点都不是我想说的,我们主要看第一点:当你把一个使用了 JSP 的项目打 war 包放到了 tomcat 或者其他容器中是可以运行的,当然你使用 java -jar 也是可以运行的,但是如果你直接执行 jar 包是不可以的
也就是说,打包的类型决定了JSP可不可以被正常解析使用,同时 SpringBoot 中 Tomcat 是嵌入式的,而 SpringBoot 程序往往又是脱离容器独立运行的,如果用 JSP ,就需要额外的地址去存生成的 Servlet ,可能会存在一定的安全问题,所默认是不支持jsp的
(3) SpringBoot推荐Thymeleaf吗?
网络上的文章都在传,SpringBoot 官方推荐 Thymeleaf,我看了一下 SpringBoot 2.2.7 版本的文档(看了下貌似2.3.0也挂成GA了,这部分没啥改变)我自己是没找到一个关于推荐确切的说法
Springboot:7.1.10. Template Engines

在Springboot——Spring Boot Features —— 7.1.10. Template Engines 中有提到,SpringBoot 提供了关于Thymeleaf 的自动装配的支持,但是同样的也提供对 FreeMarker、Groovy、Mustache 自动装配的支持,此处并没有看到推荐某一款的说法

SpringMVC:1.10.1. Thymeleaf
Thymeleaf is a modern server-side Java template engine that emphasizes natural HTML templates that can be previewed in a browser by double-clicking, which is very helpful for independent work on UI templates (for example, by a designer) without the need for a running server. If you want to replace JSPs, Thymeleaf offers one of the most extensive set of features to make such a transition easier. Thymeleaf is actively developed and maintained. For a more complete introduction, see the Thymeleaf project home page.
SpringMVC:1.10.2. FreeMarker
Apache FreeMarker is a template engine for generating any kind of text output from HTML to email and others. The Spring Framework has built-in integration for using Spring MVC with FreeMarker templates.

再看一下 SpringMVC 5.2.6.RELEASE 的文档,关于模板也没有提到支持或者推荐,而对于几种常见的例如 Thymeleaf、FreeMarker 等作出了一定的说明

所以,我个人觉得,SpringBoot 也只是提供了几种替代 JSP 的方案,并没有指定的推荐什么,当然了如果是我自己没注意到,大家也可以留言和我交流分享啊哈~
(4) 此部分小结
说了一大堆,就一个结论,SpringBoot 中不推荐 JSP 是肯定的,但是也没有指定推荐什么引擎模板,大家可以根据需要自行选择(FreeMarker、Groovy、Thymeleaf、Mustache)

Thymeleaf

A:优点
首先,配置很简单,SpringBoot 对于 Thymeleaf 在内的几种模板引擎,都提供了自动装配的支持,所以简单的引入依赖就可以快速使用
其次,Thymeleaf “很优雅” ,因为绑定或者控制的时候,都是以属性的方式,所以 HTML 的原有结构,没有被破坏掉,这样,就有了一个其他模板引擎所没有的优点:可以被浏览器正常渲染,也就是说,直接就可以访问(例如 JSP 是没有办法脱离容器直接访问的)
B:缺点
标签检查很严格,有时候会发疯…
表达式和标签种类繁多 {} 花括号前面不同的符号代表不同的意思,例如 ${…} 变量表达式 、 *{…} 选择表达式
如果习惯了 freemarker 这种类型的写法,写 Thymeleaf 会感觉很麻烦,因为两者的书写角度或者说思路是不同的
C:关于性能
关于性能,在 3.x 后 Thymeleaf 已经有了很大的提升,但是我查过一下数据,貌似仍不一定有那么理想,不过就我个人而言,我一个后端狗写页面,一堆乱七八糟的 js、css 各种增大开销,Thymeleaf 带来的一些影响,貌似与我和没有很大关系(菜是原罪)
(2) 引入Thymeleaf
用之前,肯定得引入相关的依赖对吧,可以去 Thymeleaf 官网,或者 Springboot 的官网,这里我更加推荐后者
Thymeleaf 官网:https://www.thymeleaf.org在这里插入图片描述

2.2.7 Pom

org.springframework.boot
spring-boot-starter

org.thymeleaf thymeleaf-spring5 org.thymeleaf.extras thymeleaf-extras-java8time

这样引入是一种方式,但是这些相关的东西,Springboot 都已经帮我们打包好了,开箱即用
所以最终的引入方式就是:
A:Pom 增加依赖

org.springframework.boot spring-boot-starter-thymeleaf

B:初始化时勾选相关组件在这里插入图片描述

(3) 模板页面存放位置
引入了依赖之后,先确定一下页面给放哪里,前面演示的 JSP 好不折腾,又是创建 webapp WEB-INF ,又是配置,而 Thymeleaf 等模板,则可以直接将页面放到自动生成的 templates 文件夹中就可以了
具体路径为:src – main – resources – template
如果想了解一下具体原因:可以去看一下 ThymeleafProperties 这个配置类,前后缀都已经指定好了
@ConfigurationProperties(prefix = “spring.thymeleaf”)
public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;

public static final String DEFAULT_PREFIX = “classpath:/templates/”;

public static final String DEFAULT_SUFFIX = “.html”;

// 是否在呈现模板之前检查模板是否存在
private boolean checkTemplate = true;

// 是否检查模板位置是否存在
private boolean checkTemplateLocation = true;

// 在构建URL时以查看名称作为前缀
private String prefix = DEFAULT_PREFIX;

// 在构建URL时附加到视图名称的后缀
private String suffix = DEFAULT_SUFFIX;

// 要应用于模板的模板模式
private String mode = “HTML”;

// 模板文件编码
private Charset encoding = DEFAULT_ENCODING;

(4) 入门程序体验
1、编写 Controller
@Controller
public class TestController {
@RequestMapping(“test”)
public String test(Model model){
String hello = “Hello Thymeleaf”;
model.addAttribute(“h

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值