前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目。不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/category/1657780.html

今天我们主要来看看 Thymeleaf 在 Spring Boot 中的整合!

这个系列课程的完整源码,也会提供给大家。大家关注我的微信公众号(架构师精进),回复:springboot源码 获取这个系列课程的完整源码。或者点此链接直接下载完整源码

Thymeleaf 简介

Spring Boot 2主要支持页面模板是 Thymeleaf 和 Freemarker ,当然,作为 Java 最最基本的页面模板 Jsp ,Spring Boot 也是支持的,只是使用比较麻烦。

Thymeleaf 作为新一代 Java 模板引擎,它的功能与 Velocity、FreeMarker 等传统 Java 模板引擎比较类似,但是Thymeleaf  模板后缀为 .html,可以直接被浏览器打开,因此,开发时非常方便。

它既可以让前端工程师在浏览器中直接打开查看样式,也可以让后端工程师结合真实数据查看显示效果,同时,SpringBoot 提供了 Thymeleaf 自动化配置解决方案,因此在 SpringBoot 中使用 Thymeleaf 非常方便。

事实上, Thymeleaf 除了展示基本的 HTML ,进行页面渲染之外,也可以作为一个 HTML 片段进行渲染,例如我们在做邮件发送时,可以使用 Thymeleaf 作为邮件发送模板。

整合

新项目整合 Thymeleaf 非常容易,只需要创建项目时勾上 Thymeleaf 即可,这里就不说了。

下面说说怎么在现有的项目中手动整合Thymeleaf:

1、在pom.xml 增加依赖如下

        <!-- 引入 redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.7.RELEASE</version>
</dependency>

2、application.properties 文件增加Thymeleaf 相关配置

############################################################
#
# thymeleaf 模板
#
############################################################
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
# 关闭缓存
spring.thymeleaf.cache=false
spring.thymeleaf.prefix 指定模板页面的路径

3、增加前台页面

在resource\templates\thymeleaf  目录下增加index.html 页面

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
Thymeleaf模板引擎
<h1 th:text="${name}">hello Spring Boot~~~~~~~</h1>
</body>
</html>

th:text 就是Thymeleaf的标签,用于处理标签体的文本内容。

其他更对的标签及用法,我会在下一篇文章中介绍。

注意:实际开发项目直接放resource\templates目录下就行,不需要加Thymeleaf 目录。我这里是有验证其他模板引擎框架,所以做了个目录区分。

4、创建 Controller

接下来我们就可以创建 Controller 了,实际上引入 Thymeleaf 依赖之后,我们可以不做任何配置。新建的ThymeleafController如下:

package com.weiz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import com.weiz.pojo.User; @Controller
@RequestMapping("th")
public class ThymeleafController { @RequestMapping("/index")
public String index(ModelMap map) {
map.addAttribute("name", "thymeleaf-index");
return "thymeleaf/index";
}
}

在ThymeleafController 中返回逻辑视图名,逻辑视图名为 index ,意思我们需要在 resources/templates/thymeleaf 目录下提供一个名为 index.html 的 Thymeleaf 模板文件。

注意:实际开发项目直接放resource\templates目录下就行,不需要加Thymeleaf 目录。我这里是有验证其他模板引擎框架,所以做了个目录区分。

5、运行效果

在浏览器中输入:http://localhost:8080/th/index 查看页面返回结果。

总结

主要向大家简单介绍了 Spring Boot 整合 Thymeleaf,还是比较简单的。下一篇文章会给大家详细介绍Thymeleaf的常用标签和用法。大家也可以阅读 Thymeleaf 官方文档学习 Thymeleaf 的更多用法。

这个系列课程的完整源码,也会提供给大家。大家关注我的微信公众号(架构师精进),回复:springboot源码 获取这个系列课程的完整源码。

 

SpringBoot入门系列(四)整合模板引擎Thymeleaf的更多相关文章

  1. SpringBoot入门:新一代Java模板引擎Thymeleaf(理论)

    Spring Boot 提供了spring-boot-starter-web来为Web开发予以支持,spring-boot-starter-web为我们提供了嵌入的Tomcat以及SpringMVC的 ...

  2. SpringBoot入门:新一代Java模板引擎Thymeleaf(实践)

    菜鸟教程:http://www.runoob.com/ http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js http://apps.b ...

  3. Spring Boot (四)模板引擎Thymeleaf集成

    一.Thymeleaf介绍 Thymeleaf是一种Java XML / XHTML / HTML5模板引擎,可以在Web和非Web环境中使用.它更适合在基于MVC的Web应用程序的视图层提供XHTM ...

  4. Spring Boot整合模板引擎thymeleaf

    项目结构 引入依赖pom.xml <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframew ...

  5. 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf

    ========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...

  6. SpringBoot常用Starter介绍和整合模板引擎Freemaker、thymeleaf 4节课

    1.SpringBoot Starter讲解 简介:介绍什么是SpringBoot Starter和主要作用 1.官网地址:https://docs.spring.io/spring-boot/doc ...

  7. SpringBoot入门系列(十一)统一异常处理的实现

    前面介绍了Spring Boot 如何整合定时任务已经Spring Boot 如何创建异步任务和定时任务.不清楚的朋友可以看看之前的文章:<Spring Boot 入门系列文章> 接下来主 ...

  8. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  9. SpringBoot:模板引擎 thymeleaf、ContentNegotiatingViewResolver、格式化转换器

    目录 模板引擎 thymeleaf ContentNegotiatingViewResolver 格式化转换器 模板引擎 thymeleaf.ContentNegotiatingViewResolve ...

随机推荐

  1. LGOJ4449 于神之怒加强版

    Description link 给定\(n\),\(m\),\(k\),计算 \[\sum_ {i=1}^n \sum^m_{j=1} gcd(i,j)^k \space mod \space 10 ...

  2. C#函数的基础应用

    C#函数的基础应用 函数之前的知识回顾 数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数 程序里的函数:能完成一个相对独立功能的代码模块. 数学里的函数:高度抽象. 函 ...

  3. E. Tree Painting(树形换根dp)

    http://codeforces.com/contest/1187/problem/E 分析:问得分最高,实际上就是问以哪个节点出发得到的分数最多,而呈现成代码形式就变成了换根,max其得分!!!而 ...

  4. 学习python-20191230(1)-Python Flask高级编程开发鱼书_第04章_应用、蓝图与视图函数

    视频06: 1.自动导包快捷键——默认为alt + enter 键组合          选中的字符由小写变为大写——Ctrl + Shift + U键组合 2.DataRequired()——防止用 ...

  5. python运行报错——注释报错

    本人是IT行业的,从事软件测试,还是个菜鸟.希望大神们多多关照~ 首先,开通这个博客的目的: 1)通常我容易犯一些低级的错误,而且在网上找到解决方法,解决之后时间长了又不记得: 2)想和有共同兴趣的人 ...

  6. 在virtualenv中安装NumPy、 SciPy、 scikit-learn、 matplotlib

    首先要进入对应的虚拟环境 然后安装包    这里把安装源改成使用豆瓣的源进行下载  这样的话 下载速度会快很多   安装numpy包 pip install numpy -i https://pypi ...

  7. isdigital()函数

    函数说明: 主要用于检查其参数是否为十进制数字字符. 头文件: C——#include<ctype.h> C++——#include<cctype> 函数定义:  int is ...

  8. valgrind 的使用简介

    zz自 http://blog.csdn.net/destina/article/details/6198443  感谢作者的分享! 一  valgrind是什么? Valgrind是一套Linux下 ...

  9. 【clientX,offsetX,screenX】 【scrollWidth,clientWidth,offsetWidth】的区别

    一.深刻认识clientX,offsetX,screenX 概念(来源于网络): clientX 设置或获取鼠标指针位置相对于当前窗口的 x 坐标,其中客户区域不包括窗口自身的控件和滚动条. clie ...

  10. mysql表关联问题(第三卷:外键多对多)

    现在我们整理一下多对多的问题,举个例子现在一个男的可能和多个女的谈过恋爱,一个女的也可能和多个男的谈过恋爱,把他们恋爱的关系整理为数据关联表就成为了多对多的关系. 准备三张表,男人信息表,女人信息表, ...