Vert.x Thymeleaf 模板引擎入门

编程教程 > Java > Vert.x (277) 2024-11-26 14:39:04

前言

创建一个常规的web项目肯定需要一个模板引擎来实现,引用之前的项目《Vert.x 4 Web应用初识》基础结构

目前vert.x支持多款模板引擎,包括:

  • MVEL template engine
  • Jade template engine
  • Handlebars template engine
  • Thymeleaf template engine
  • Apache FreeMarker template engine
  • Pebble template engine
  • Rocker template engine
  • HTTL template engine
  • Rythm template engine
  • JTE template engine

这里主要讲解一个Java编程熟悉的模板引擎Thymeleaf 

项目结构

Vert.x Thymeleaf 模板引擎入门_图示-4ee1c543d4b84aedafb592f760b1cf70.png
项目结构

引入模板引擎依赖

maven pom.xml

<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-thymeleaf</artifactId>
<version>4.5.9</version>
</dependency>

 

Vert.x Thymeleaf模板引擎使用

 

创建模板引擎和模板引擎处理器

这里模板引擎以Thymeleaf为例,编辑MainVerticle

代码聚焦

    TemplateEngine templateEngine= ThymeleafTemplateEngine.create(vertx);
TemplateHandler templateHandler = TemplateHandler
.create(templateEngine);
// .create(templateEngine,"templates","text/html");

 

单个路径映射和处理

代码聚焦

//路径模板一对一映射
router.get("/about.html").handler(ctx->{
ctx.put("hello","hello world");
templateEngine.render(ctx.data(),"templates/about.html",bufferAsyncResult -> {
// 如果模板解析成功,就将结果写到response
if (bufferAsyncResult.succeeded()) {
ctx.response().putHeader("Content-Type", "text/html").end(bufferAsyncResult.result());
} else { // 如果解析失败,就显示fail
ctx.fail(bufferAsyncResult.cause());
}
});
});
<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>about</title>
</head>
<body>
<h1 th:text="${hello}"></h1>
<th:block th:insert="~{templates/common.html::desc}"></th:block>
</body>
</html>
 
<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="desc">
<p>
我是一段简单的介绍,每个页面都可以th:insert="~{common::desc}"引入哟
</p>
</th:block>
 

注意上方代码,暂未使用templateHandler

使用浏览器访问 http://localhost:8080/about.html

Vert.x Thymeleaf 模板引擎入门_图示-0bab0cf080e14e93a67fc9d7af4dc41c.png

访问结果与预期一致。

 

不支持以下写法【红色提示】

router.get("/address.html").handler(ctx->{
ctx.put("hello","hello world");
ctx.next();
}).handler(templateHandler);

以上写法并不能将 /address.html 映射到 templates目录下address.html文件。

源码剖析

Vert.x Thymeleaf 模板引擎入门_图示-f040a096729348d88515f49de1b5a076.png
TemplateHandlerImpl实现类

TemplateHandlerImpl实现类中调用了一个处理文件路径的方法

Vert.x Thymeleaf 模板引擎入门_图示-22ec6f98dadf4ee6a923a9670ed8ddb0.png
文件处理工具

方法中有个坑,非正则匹配的这里会截取路径全部,导致上面的file拿到 “”空串然后 TemplateHandlerImpl 调用 模板引擎渲染时候路径拼接无法找到

Vert.x Thymeleaf 模板引擎入门_图示-520f27bbdcc14f7d8cd7e20385e80d70.png

 

正则匹配同时处理多个路径模板

上面一对一没用到TemplateHandler 

TemplateHandler templateHandler = TemplateHandler
.create(templateEngine);

这里正则匹配多个会用到,下面为正则匹配对应路径开头的最后都要走模板解析器处理

//指定目录映射动态模板
router.get("/dynamic/*").last().handler(templateHandler);

//指定后缀映射动态模板
router.getWithRegex(".+\\.html").last().handler(templateHandler);

注意:必须加last()排序到最后,否则可能设定了参数因排序问题导致解析失败或报错。

对应目录使用路径参考代码

router.get("/product/detail.html").handler(ctx->{
ctx.put("hello","hello world");
ctx.next();
});
<!DOCTYPE html>
<html lang="zh" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>product - detail</title>
</head>
<body>
<h1 th:text="${hello}"></h1>
<th:block th:insert="~{templates/common.html::desc}"></th:block>
</body>
</html>
 

上面是个.html结尾的,受到上方第二个规则匹配,所以最后会渲染成对应的html,html模板位置为 templates/product/detail.html (可以参考上面的源码剖析部分得到为啥是这个路径)

product/detail.html 模板存放位置参考

Vert.x Thymeleaf 模板引擎入门_图示-3d67db9088e3413989ec86d840711ed4.png
product/detail.html 模板存放位置

 

使用浏览器访问 http://localhost:8080/product/detail.html

Vert.x Thymeleaf 模板引擎入门_图示-7314fd0805d240789bfe22392a280381.png
产品详情页面

从上图来看和我们预期结果一致。

 

至此我们学会了在Vert.x 4中使用模板引擎基础

 


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
前言创建一个常规的web项目肯定需要一个模板引擎来实现,引用之前的项目《Vert.x 4 Web应用初识》基础结构目前vert.x支持多款模板引擎,包括:MVE
Vert.x java 入门,Vert.x这个框架在常规的web业务开发中估计还不是很成熟。但是了解了他的一些原理我觉得可以有一番作为。今天主要简单讲解下eclipse Vert.x是什么有什么...
前言项目由之前的第一个vert.x 4 项目改编而来,vert.x项目创建参考: vert.x 4 web应用编写修改MainVerticle文件,内容如下:p
前言最新的Ver.x 4 从idea工具运行启动,并访问。项目创建通过vert.x官网生成器完成。Vert.x 4项目创建打开vert.x官网项目生成地址,ht
前言Vert.x Router路由中多个处理器之间通过RoutingContext共享数据实现。 Vert.x Router 上下文数据数据设置routingC
前言这里主要讲解Vert.x配置文件的读取和使用,引用之前的项目《Vert.x 4 Web应用初识》 引入必要依赖maven pom.xml&lt;dependenc
前言项目创建参考之前的《Vert.x 4 Web应用初识》。本文通过Vert.x实现了REST接口的CRUD操作。通过本教程你可以获得以下内容vert.x项目中
前言引用《Vert.x 4 Web REST CRUD接口应用》项目,加入日志依赖并编码实现类似Interceptor功能处理。vert.x日志集成参考《Ver
前言文件上传在web应用比较常见,本文以vert.x web实现文件上传功能。引用之前的项目《Vert.x 4 Web应用初识》作为基础,添加了日志。 Vert
前言Vert.x 中实现全局数据共享,如环境参数等。Vertx数据共享实现共享数据存入/更新SharedData sharedData = vertx.shar
前言本文主要讲解在Vert.x环境下与Mysql数据库连接和操作,实现基础的增删改查和事务,以及REST接口返回数据库中查询结果。项目引用之前的《Vert.x
前言接上一篇《Vert.x 4 Web应用初识》,在web应用中除了访问地址得到动态的信息还有静态的资源库访问,如 jQuery / bootstrap 等前端
前言Web项目开发一般接口入参都有校验需求,Vert.x 4 目前已有插件实现参数校验,引用之前的项目《Vert.x 4 Web应用初识》源码 引入验证插件ma
前言vert.x 默认是没有像spring的依赖注入的,需要自己结合vertx-service-proxy插件实现。本文引用项目为基础《Vert.x 4 Web
前言前面已经学习了Vert.x web的基础接口,本文主要讲解引入jwt为接口认证/鉴权。引用之前创建的项目《Vert.x 4 Web应用初识》,加入jwt t