号外:为读者持续整理了几份最新教程,覆盖了 Spring Boot、Spring Cloud、微服务架构等PDF。
获取方式:关注右侧公众号"泥瓦匠BYSocket",来领取吧!


摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢!

这是泥瓦匠的第105篇原创

文章工程:
* JDK 1.8
* Maven 3.5.2
* Spring Boot 2.1.3.RELEASE
* 工程名:springboot-webflux-4-thymeleaf
* 工程地址:见文末

前言

上一讲,我们用 MongoDB 来实现 WebFlux 对数据源的操作。那么有了数据需要渲染到前台给用户展示。这就是本文关心的 View 层。View 的表现形式有很多,比如 JSON 和 HTML。开发中常用模板语言很常见的有 Thymeleaf、Freemarker等。那

什么是模板语言?

常见的模板语言都包含以下几个概念:数据(Data)、模板(Template)、模板引擎(Template Engine)和结果文档(Result Documents)。

  • 数据

数据是信息的表现形式和载体,可以是符号、文字、数字、语音、图像、视频等。数据和信息是不可分离的,数据是信息的表达,信息是数据的内涵。数据本身没有意义,数据只有对实体行为产生影响时才成为信息。

  • 模板

模板,是一个蓝图,即一个与类型无关的类。编译器在使用模板时,会根据模板实参对模板进行实例化,得到一个与类型相关的类。

  • 模板引擎

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

  • 结果文档

一种特定格式的文档,比如用于网站的模板引擎就会生成一个标准的HTML文档。

模板语言用途广泛,常见的用途如下:

  • 页面渲染
  • 文档生成
  • 代码生成
  • 所有 “数据+模板=文本” 的应用场景

Spring Boot 推荐使用的模板语言是 Thymeleaf,那

什么是 Thymeleaf?

官方的解释如下:

Thymeleaf is a modern server-side Java template engine for both web and standalone environments.

Thymeleaf 是现代的模板语言引擎,可以独立运行也可以服务于 Web。主要目标是为开发提供天然的模板,并且能在 HTML 里面准确的显示。

Thymeleaf 是新一代 Java 模板引擎,在 Spring 4 后推荐使用。目前是 Spring 5 自然更加推荐。

结构

类似上面讲的工程搭建,新建一个工程编写此案例。工程如图:

目录如下

  • org.spring.springboot.webflux.controller – Controller 层
  • org.spring.springboot.dao – 数据操作层 DAO
  • org.spring.springboot.domain – 实体类
  • org.spring.springboot.handler – 业务逻辑层
  • Application – 应用启动类
  • application.properties – 应用配置文件
  • pom.xml maven 配置
  • application.properties 配置文件

模板是会用到下面两个目录

  • static 目录是存放 CSS、JS 等资源文件
  • templates 目录是存放视图

本文重点在 Controller 层 和 templates 视图的编写。

新增 POM 依赖与配置

在 pom.xml 配置新的依赖:

  <dependencies>

    <!-- Spring Boot Web Flux 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <!-- 模板引擎 Thymeleaf 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>

这里我们增加了 Thymeleaf 依赖,但不用在 application.properties – 应用配置文件 配置人任何配置。默认启动其默认配置,如需修改配置参考 Thymeleaf 依赖配置,如下:

spring.thymeleaf.cache=true # Enable template caching.
spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
spring.thymeleaf.check-template-location=true # Check that the templates location exists.
spring.thymeleaf.enabled=true # Enable Thymeleaf view resolution for Web frameworks.
spring.thymeleaf.encoding=UTF-8 # Template files encoding.
spring.thymeleaf.excluded-view-names= # Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.mode=HTML5 # Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
spring.thymeleaf.reactive.max-chunk-size= # 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 that can be resolved.

包括常用的 编码、是否开启缓存等等。

WebFlux 中使用 Thymeleaf

在 CityWebFluxController 控制层,添加两个方法如下:

    @GetMapping("/hello")
public Mono<String> hello(final Model model) {
model.addAttribute("name", "泥瓦匠");
model.addAttribute("city", "浙江温岭"); String path = "hello";
return Mono.create(monoSink -> monoSink.success(path));
} private static final String CITY_LIST_PATH_NAME = "cityList"; @GetMapping("/page/list")
public String listPage(final Model model) {
final Flux<City> cityFluxList = cityHandler.findAllCity();
model.addAttribute("cityList", cityFluxList);
return CITY_LIST_PATH_NAME;
}

解释下语法:

  • 返回值 Mono 或者 String 都行,但是 Mono 代表着我这个返回 View 也是回调的。
  • return 字符串,该字符串对应的目录在 resources/templates 下的模板名字。
  • Model 对象来进行数据绑定到视图
  • 一般会集中用常量管理模板视图的路径

Tymeleaf 视图

然后编写两个视图 hello 和 cityList,代码分别如下:

hello.html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8"/>
<title>欢迎页面</title>
</head> <body> <h1 >你好,欢迎来自<p th:text="${city}"></p>的<p th:text="${name}"></p></h1> </body>
</html>

cityList.html:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8"/>
<title>城市列表</title>
</head> <body> <div> <table>
<legend>
<strong>城市列表</strong>
</legend>
<thead>
<tr>
<th>城市编号</th>
<th>省份编号</th>
<th>名称</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr th:each="city : ${cityList}">
<td th:text="${city.id}"></td>
<td th:text="${city.provinceId}"></td>
<td th:text="${city.cityName}"></td>
<td th:text="${city.description}"></td>
</tr>
</tbody>
</table> </div> </body>
</html>

常用语法糖如下

  • ${…} 变量表达式
  • th:text 处理 Tymeleaf 表达式
  • th:each 遍历表达式,可遍历的对象:实现java.util.Iterable、java.util.Map(遍历时取java.util.Map.Entry)、array 等

还有很多使用参考官方方文档 http://www.thymeleaf.org/documentation.html

运行工程

下面运行工程验证下。使用 IDEA 右侧工具栏,点击 Maven Project Tab ,点击使用下 Maven 插件的 install 命令。或者使用命令行的形式,在工程根目录下,执行 Maven 清理和安装工程的指令:

cd springboot-webflux-4-thymeleaf
mvn clean install

在控制台中看到成功的输出:

... 省略
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:30 min
[INFO] Finished at: 2017-10-15T10:00:54+08:00
[INFO] Final Memory: 31M/174M
[INFO] ------------------------------------------------------------------------

在 IDEA 中执行 Application 类启动,任意正常模式或者 Debug 模式。可以在控制台看到成功运行的输出:

... 省略
2018-04-10 08:43:39.932 INFO 2052 --- [ctor-http-nio-1] r.ipc.netty.tcp.BlockingNettyContext : Started HttpServer on /0:0:0:0:0:0:0:0:8080
2018-04-10 08:43:39.935 INFO 2052 --- [ main] o.s.b.web.embedded.netty.NettyWebServer : Netty started on port(s): 8080
2018-04-10 08:43:39.960 INFO 2052 --- [ main] org.spring.springboot.Application : Started Application in 6.547 seconds (JVM running for 9.851)

打开浏览器,访问 http://localhost:8080/city/hello ,可以看到如图的响应:

继续访问 http://localhost:8080/city/page/list , 发现没有值,那么按照上一讲插入几条数据即可有值,如图:

总结

这里,探讨了 Spring WebFlux 的如何整合 Thymeleaf 。整合其他模板语言 Thymeleaf、Freemarker,就大同小异了。下面,我们能会整合 Thymeleaf 和 MongoBD,实现一个整体的简单案例。

代码示例

本文示例读者可以通过查看下面仓库的中的模块工程名: 2-x-spring-boot-webflux-handling-errors:

如果您对这些感兴趣,欢迎 star、follow、收藏、转发给予支持!

参考资料

  • Spring Boot 2.x WebFlux 系列:https://www.bysocket.com/archives/2290
  • spring.io 官方文档

以下专题教程也许您会有兴趣

​ 
(关注微信公众号,领取 Java 精选干货学习资料) 
(添加我微信:bysocket01。加入纯技术交流群,成长技术)

Spring Boot 2 快速教程:WebFlux 集成 Thymeleaf(五)的更多相关文章

  1. Spring Boot 2 快速教程:WebFlux 集成 Mongodb(四)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第104篇原创 文章工程:* JDK 1.8* M ...

  2. Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 这是泥瓦匠的第102篇原创 03:WebFlux Web CR ...

  3. Spring Boot 2 快速教程:WebFlux 快速入门(二)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 02:WebFlux 快速入门实践 文章工程: JDK 1.8 ...

  4. Spring Boot 2.x教程-Thymeleaf 原理是什么

    layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boo ...

  5. Spring Boot:快速入门教程

    什么是Spring Boot? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人 ...

  6. Spring Boot 如何快速集成 Redis 哨兵?

    上一篇:Spring Boot 如何快速集成 Redis? 前面的分享栈长介绍了如何使用 Spring Boot 快速集成 Redis,上一篇是单机版,也有粉丝留言说有没有 Redis Sentine ...

  7. Spring Boot【快速入门】

    Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point for building all Sp ...

  8. 【转载】Spring Boot【快速入门】2019.05.19

    原文出处:https://www.cnblogs.com/wmyskxz/p/9010832.html   Spring Boot 概述 Build Anything with Spring Boot ...

  9. Spring Boot【快速入门】简单案例

    Spring Boot[快速入门]   Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...

随机推荐

  1. django基础之day08,ajax结合sweetalert的使用

    models.py文件 from django.db import models class User(models.Model): username=models.CharField(max_len ...

  2. Python的小整数池

    此处经常会作为面试题!!! 小整数池目的:节省内存,提高执行效率 需要注意的是:Python实现int的时候有个小整数池.为了避免因创建相同的值而重复申请内存空间所带来的效率问题, Python解释器 ...

  3. Docker入门-笔记-1

    Docker入门 Docker 是 Golang 编写的, 自 2013 年推出以来,受到越来越多的开发者的关注.如果你关注最新的技术发展,那么你一定听说过 Docker.不管是云服务还是微服务(Mi ...

  4. C# 只读模式读取txt文件内容

    读取txt文件时,提示异常: 文件“..\Log\all_info.txt”正由另一进程使用,因此该进程无法访问此文件 原因: 日志文件通过lognet生成的日志文件(C#使用log4net记录日志) ...

  5. 现代前端库开发指南系列(二):使用 webpack 构建一个库

    前言 在前文中,我说过本系列文章的受众是在现代前端体系下能够熟练编写业务代码的同学,因此本文在介绍 webpack 配置时,仅提及构建一个库所特有的配置,其余配置请参考 webpack 官方文档. 输 ...

  6. Cobbler 2.x安装与配置

    软件环境 Centos7.3 Cobbler 2.8.1 1.下载.编译和安装 创建自动安装脚本cobbler-install.sh #!/bin/bash # File Name: /data/sr ...

  7. Dc:7 Vulnhub Walkthrough

    靶机下载地址: https://www.vulnhub.com/entry/dc-7,356/ 主机扫描: http://10.10.202.161/ Google搜索下: SSH 登录 以上分析得出 ...

  8. CentOS 7 离线环境安装nginx时报错:./configure: error: C compiler cc is not found

    先说解决方法: 在nginx目录下,查看objs/autoconf.err文件,该文件记录了具体的错误信息 vi objs/autoconf.err 一般就是缺少一些文件,因为我的gcc.g++也是离 ...

  9. python-基础r/R、b、u/U含义

    1.r/R,代表非转义的原始字符串,一般使用在正则表达式和win目录上 2.b“” 代表b后面的内容为bytes类型 3.u/U 表示对字符串进行unicode编码,一般使用在有中午的地方,防止乱码.

  10. [小技巧]你真的了解C#中的Math.Round么?

    今天在某.NET Core 群中看到有人在问Math.Round的问题.其实这个问题之前有很多人遇到了,在此总结一下. 开发者为了实现小数点后 2 位的四舍五入,编写了如下代码, var num = ...