目录

Thymeleaf 模板引擎概述

Thymeleaf 官方文档下载

Spring Boot 渲染 Thymeleaf  规则

Spring Boot 集成 Thymeleaf  快速入门

静态资源映射规则

配置应用首页

WebJars 管理前端 jar 版本


Thymeleaf 模板引擎概述

1、以前开发 web 项目时,只需将静态的 ".html” 页面后缀名修改为“.jsp”,然后在文件中加入 jsp 页面标识即可做 jsp 开发,然而 Spring Boot 项目采用打 jar 包的方式,默认使用的是内置的 Tomcat 服务器,所以默认是不支持 jsp 的,但可以使用其它的模板引擎。

2、市面上主流的 Java 模板引擎有:JSP、Velocity、Freemarker、Thymeleaf 等等。

3、JSP 本质也是模板引擎,Spring Boot 官方支持:Thymeleaf Templates、FreeMarker Templates、Groovy Templates 等模板引擎。

4、模板引擎原理图如下,模板引擎的作用都是将模板(页面)和数据进行整合然后输出显示,区别在于不同的模板使用不同的语法,如 JSP 的 JSTL 表达式,以及 JSP 自己的表达式和语法,同理 Thymeleaf 也有自己的语法.

Thymeleaf 是 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理HTML,XML,JavaScript,CSS 甚至纯文本。
Thymeleaf 的主要目标是提供一种优雅和高度可维护的创建模板的方式。为了实现这一点,它建立在自然模板的概念上,将其逻辑注入到模板文件中,不会影响模板被用作设计原型。这改善了设计的沟通,弥补了设计和开发团队之间的差距。
Thymeleaf 也从一开始就设计了Web标准 - 特别是 HTML5 - 允许您创建完全验证的模板,Spring Boot 官方推荐使用  thymeleaf 而不是 JSP。
Spring Boot 中使用 Thymeleaf  模板引擎时非常简单,因为 Spring Boot 已经提供了默认的配置,比如解析的文件前缀,文件后缀,文件编码,缓存等等,程序员需要的只是写 html 中的内容即可。

Thymeleaf 官网:Thymeleaf

https://github.com/thymeleaf/thymeleaf

Thymeleaf 官方文档下载

https://www.thymeleaf.org/documentation.html

Spring Boot 渲染 Thymeleaf  规则

1、渲染流程规则可以从它的自动配置属性文件 org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 查看:

/*** Properties for Thymeleaf.* @author Stephane Nicoll* @author Brian Clozel* @author Daniel Fernández* @author Kazuki Shimizu* @since 1.2.0*/
@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";/*** Whether to check that the template exists before rendering it.*/private boolean checkTemplate = true;
  • 默认前缀:DEFAULT_PREFIX = "classpath:/templates/"
  • 默认后缀:DEFAULT_SUFFIX = ".html"

2、所以默认只要把 HTML 页面放在 classpath:/templates/ 下,thymeleaf 模板引擎就能自动渲染, classpath:/templates/ 目录以外的 html 文件是无法使用 Thymeleaf 引擎的。

3、可以在全局配置文件中修改这些规则, 官方文档配置:

# THYMELEAF (ThymeleafAutoConfiguration)
spring.thymeleaf.cache=true # Whether to enable template caching.
spring.thymeleaf.check-template=true # Whether to check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Whether to check that the templates location exists.
spring.thymeleaf.enabled=true # Whether to enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.enable-spring-el-compiler=false # Enable the SpringEL compiler in SpringEL expressions.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names (patterns allowed) that should be excluded from resolution.
spring.thymeleaf.mode=HTML # Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.reactive.chunked-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be the only ones executed in CHUNKED mode when a max chunk size is set.
spring.thymeleaf.reactive.full-mode-view-names= # Comma-separated list of view names (patterns allowed) that should be executed in FULL mode even if a max chunk size is set.
spring.thymeleaf.reactive.max-chunk-size=0 # Maximum size of data buffers used for writing to the response, in bytes.
spring.thymeleaf.reactive.media-types= # Media types supported by the view technology.
spring.thymeleaf.servlet.content-type=text/html # Content-Type value written to HTTP responses.
spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
spring.thymeleaf.template-resolver-order= # Order of the template resolver in the chain.
spring.thymeleaf.view-names= # Comma-separated list of view names (patterns allowed) that can be resolved.

4、templates 为模板目录,只有使用 templateaf 模板引擎时才有用,模板目录中的文件(如 *.html)从浏览器是无法直接访问的(index.html 首页地址除外),相当于以前的 WEB-INF目录下的文件无法直接访问一样,必须通过后台才能访问。它'静态资源目录'不一样,静态资源目录中的文件从浏览器是可以直接访问的,而且与模板引擎无关。

Spring Boot 集成 Thymeleaf  快速入门

1、使用 Thymeleaf 同样第一步在 pom.xml 引入  spring-boot-starter-thymeleaf 模块(Starters):

        <!-- 引入thymeleaf模板引擎--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

pom.xml · 汪少棠/jpaTransactional - Gitee.com

如果在 IDEA 创建项目的时候勾选了 Thymeleaf 模板引擎依赖,则 IDEA 构建项目的时候自动会在 resources 下新建 templates 模板目录,没有时也没关系,手动新建即可。

2、后台控制台提供一个接口,浏览器访问后,跳转到模板目录下的 .html 文件,同时向他传递数据过去显示。

    /*** http://localhost:8080/thymeleaf/home** @param responseMap* @return*/@RequestMapping("thymeleaf/home")public String goHome(Map<String, Object> responseMap, Model model) {// 向页面返回数据方式1:默认 responseMap 的内容会放到请求域中,调整后的新页面上可以直接使用 Thymeleaf 表达式取值responseMap.put("name", "张三");responseMap.put("age", 35);// 向页面返回数据方式2:使用 org.springframework.ui.Model 向页面返回数据model.addAttribute("code", 200);model.addAttribute("msg", "管理员向你表示祝贺!");// 全部基于 Spring Boot 给 Thymeleaf 的默认配置// 自动跳转到默认的 classpath:/templates/home.html 页面return "home";}

src/main/java/com/wmx/controller/ThymeleafController.java · 汪少棠/jpaTransactional - Gitee.com

3、前端 .html 页面中的 <html> 标签加上 xmlns:th="http://www.thymeleaf.org" 属性,IDEA 编辑器就会有 Thymeleaf 语法提示,不写不影响运行。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en"><meta charset="UTF-8"><title>主页</title>
</head>
<body>
<h3>Spring Boot 集成 Thymeleaf 快速入门 </h3>
<!--Thymeleaf 语法取值--><p>姓名:<span th:text="${name}">未知</span></p>
<p>年龄:[[${age}]]</p><p>状态码:<span th:text="${code}"></span></p>
<p>消息:[[${msg}]]</p></body>
</html>

src/main/resources/templates/home.html · 汪少棠/jpaTransactional - Gitee.com

th:text 是 Thymeleaf 其中的一个取值语法之一,关于 Thymeleaf 的详细语法可以参考 hymeleaf


静态资源映射规则

本节与 Thymeleaf 知识点没有关系,是单独的知识点。

1、Spring boot 静态资源映射规则可以在“org.springframework.boot.autoconfigure.web”包下面的“ResourceProperties”类中找到

@ConfigurationProperties(prefix = "spring.resources",    ignoreUnknownFields = false
)
public class ResourceProperties {
//可以设置和静态资源有关的参数,如缓存时间等

2、ResourceProperties 类中约定如下,即应用中的静态资源默认都是从类路径下的以下约定目录中按优先级顺序进行寻找:

 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {"classpath:/META-INF/resources/", "classpath:/resources/","classpath:/static/", "classpath:/public/" };/*** Locations of static resources. Defaults to classpath:[/META-INF/resources/,* /resources/, /static/, /public/].*/private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

3、当浏览器页面需要获取静态资源时,会默认按照约定进行逐个查找,直到找到或者全部检索完,优先级从上至下由高到低:

  • "classpath:/META‐INF/resources/",
  • "classpath:/resources/",
  • "classpath:/static/",
  • "classpath:/public/"

4、如果想要修改静态资源位置,则可以使用 spring.resources.static-locations 配置(更多 spring.resources.* 配置参考官网):

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

配置应用首页

本节与 Thymeleaf 知识点没有关系,是单独的知识点。

1、欢迎页即应用首页,默认映射静态资源文件夹下的 index.html 页面作为首页。

2、“localhost:8080/应用上下文路径 ” 此时默认找 index.index 页面作为首页

3、四大静态资源目录下的资源,浏览器都可以直接访问,其中默认以 index.html 为应用首页,当引入了 thymeleaf 模板引擎后,应用首页 index.html 也可以放在 templates 目录下,浏览器输入 "http://ip:port/应用上下文" 同样可以进入。

4、templates 为模板目录,其中的文件(如 *.html)从浏览器是无法直接访问的(index.html除外),相当于以前的 WEB-INF目录下的文件无法直接访问一样,必须通过后台才能访问。

WebJars 管理前端 jar 版本

本节与 Thymeleaf 知识点没有关系,是单独的知识点。

1、Web 前端使用了越来越多的JS或CSS,如 jQuery、Bootstrap 等,一般情况下,将这些 Web 资源拷贝到 Java Web 项目的webapp 相应目录下进行管理。

2、WebJars 是将 web 前端资源(js,css等)打成 jar 包文件,然后借助 Maven 工具,以 jar 包形式对 web 前端资源进行统一依赖管理,保证这些 Web 资源版本唯一性。

3、WebJars 的 jar 包部署在 Maven 中央仓库上,可以从 WebJars 官网下载:WebJars - Web Libraries in Jars

4、使用起来非常简单,就像后台依赖一样,在 pom.xml 中导入前端依赖即可,下面以 JQuery 为例:

5、“META-INF/resources” 目录是 Spring Boot 约定的类路径下的静态资源默认访问目录,所以访问 webJars 的时候不要再路径中加上前缀 “META-INF/resources”,而是直接从“webjars”层级开始。

Spring Boot 集成 Thymeleaf 快速入门、静态资源映射规则、WebJars相关推荐

  1. html资源文件放在哪里,09 Spring Boot开发web项目之静态资源放哪里?

    Spring Boot开发web项目之静态资源放哪里? 先了解自动装配autoconfiguration 这些内容是spring boot天然集成好的框架 找到WebMvcAutoConfigrati ...

  2. Spring Boot:(五)静态资源和拦截器处理

    Spring Boot:(五)静态资源和拦截器处理 前言 本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类WebMvcConfigurerAdapter. 正文 前面章节我们也有 ...

  3. Spring Boot集成Thymeleaf模板引擎

    一.Thymeleaf 模板介绍 Spring Boot 推荐使用Thymeleaf 来代替传统开发中的JSP,那么什么是Thymeleaf 模板引擎呢?下面就来简单的介绍一下. Thymeleaf ...

  4. Spring Boot 引入 Thymeleaf 及入门使用

    目录: 引言 1.Spring Boot 引入 Thymeleaf 1.1 修改 Thymeleaf 版本 1.2 修改 Thymeleaf Layout Dialect 版本 2.Thymeleaf ...

  5. js文件中怎么使用thymeleaf标签_007、Spring Boot集成Thymeleaf模板引擎

    1. Thymeleaf 介绍 Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎. Thymeleaf 的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏 ...

  6. Spring Boot静态资源映射规则

    目录 理论 演示 理论 使用Spring Boot: 1. 创建Spring Boot应用,选中需要的模块: 2. Spring Boot已默认将这些场景配置好,只需要在配置文件中指定少量配置就可以运 ...

  7. Spring Boot怎么样处理静态资源(静态资源映射规则)_Web开发

    文章目录 一.SpringBoot对静态资源的映射规则 1.所有 /webjars/**,都去 classpath:/META-INF/resources/webjars/ 找资源 2. " ...

  8. Spring Boot集成thymeleaf异步刷新页面

    现在比较流行前后端分离开发,但在有些业务场景下模板引擎也用的不少.本文介绍thymeleaf页面的局部更新,Spring Boot采用的是2.0.4,先来看代码. IndexController.ja ...

  9. SpringBoot_web开发-webjars静态资源映射规则

    现在要做WEB功能,还是选择WEB模块,pom文件依赖web模块,<dependency><!-- 引入web模块 --><groupId>org.springfr ...

  10. web静态资源访问规则||webjars的访问配置——webjars是maven库里面对css js image打的一个jar包

    Html css js image  txt   web项目中 放在 Webapp 在springboot项目中  静态资源放置的位置 Springboot默认的静态资源目录 (1)在src/main ...

最新文章

  1. batch 批处理获取系统时间
  2. MySQL -Naivacat工具与pymysql模块
  3. 收藏一些自己认为好的网站或博客
  4. mysql主从技术_MySQL主从架构的实现
  5. 第六十二期:看完这篇还不了解Nginx,那我就哭了!
  6. Linux驱动小技巧 | 利用DRIVER_ATTR实现调用内核函数
  7. java上传kafka的方法_哪种方法是将所有数据从Kafka主题复制到接收器(文件或Hive表)的最佳方法?...
  8. qt 5.0中HeaderView的setResiziMode无法使用的问题
  9. Final关键字和类的自动加载
  10. 2977,3110 二叉堆练习1,3——codevs
  11. vue让元素固定_vue 监听dom元素的滚动事件 实现某元素吸顶或者固定位置显示
  12. matlab中3乘4魔方阵,小代码3 魔方矩阵
  13. Win32屏幕保护程序
  14. jndi weblogic mysql_WebLogic使用总结(三)——WebLogic配置JNDI数据源
  15. 软件评测师--第11小时 安全测试和评估
  16. Frank Pfenning
  17. js正则表达式的创建、边界符、量词符
  18. 算术左、右移位与逻辑左、右移位,右移一位和除二的区别、算术溢出
  19. 【Demo见真章】投稿赢HarmonyOS手机Beta公测名额
  20. unity中使用手柄控制角色移动

热门文章

  1. 拓端tecdat|R语言用相关网络图可视化分析汽车配置和饮酒习惯
  2. 拓端tecdat|R语言时变面板平滑转换回归模型TV-PSTR分析债务水平对投资的影响
  3. c++下字符串分割函数split实现
  4. (12)数据结构-二叉树基本操作
  5. 响应服务器589,示例HTTP范围请求会话
  6. jQuery学习笔记03
  7. python sorted函数
  8. 如何用yolov5测试图片
  9. python生成指定年份所有的天,并计算每天属于一年的第几周和周几
  10. java数组使用实验报告_Java课程实验报告实验六——异常处理