vert.x学习(三),Web开发之Thymeleaf模板的使用
在vert.x中使用Thymeleaf模板,需要引入vertx-web-templ-thymeleaf依赖。pom.xml文件如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.javafm</groupId>
<artifactId>vertx.helloworld</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-templ-thymeleaf</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
同前面一篇文章一样,不需要引入core和web了,vertx-web-templ-thymeleaf会自动导入相关依赖。
编写模板文件resources/templates/hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1 th:text="${msg}"></h1>
</body>
</html>
编写vertx http服务,并解析模板,HelloThymeleaf.java
package com.javafm.vertx.helloworld; import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.templ.ThymeleafTemplateEngine; /**
* Created by lemontea <36634584@qq.com> on 16-12-19.
*/
public class HelloThymeleaf {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
HttpServer httpServer = vertx.createHttpServer();
Router router = Router.router(vertx);
// 创建一个模板引擎
ThymeleafTemplateEngine templateEngine = ThymeleafTemplateEngine.create();
// 设置访问路径
router.route("/hello").handler(routingContext -> {
// 传递值到模板中,在模板中可以通过${msg}直接取出
routingContext.put("msg", "Hello Thymeleaf!");
// 渲染模板
templateEngine.render(routingContext, "templates/hello.html", res -> {
// 如果模板解析成功,就将结果写到response
if (res.succeeded()) {
routingContext.response().putHeader("Content-Type", "text/html").end(res.result());
} else { // 如果解析失败,就显示fail
routingContext.fail(res.cause());
}
});
});
httpServer.requestHandler(router::accept).listen(8080);
}
}
启动http服务,到浏览器查看结果

原创文章,转载请注明出处。
vert.x学习(三),Web开发之Thymeleaf模板的使用的更多相关文章
- Web开发之Tomcat&Servlet
<!doctype html>01 - JavaEE - Tomcat&Servlet figure:first-child { margin-top: -20px; } #wri ...
- 移动web开发之rem适配布局
移动web开发之rem适配布局 方案: 页面布局文字能否随着屏幕大小变化而变化 流式布局和flex布局主要针对于宽度布局,那高度如何布局? 怎样让屏幕发生变化的时候元素高度和宽度等比例缩放? 1. r ...
- Vim下的Web开发之html,CSS,javascript插件
Vim下的Web开发之html,CSS,javascript插件 HTML 下载HTML.zip 解压HTML.zip,然后将里面的所有文件copy到C:\Program Files\Vim\vi ...
- SpringBoot Web开发(4) Thymeleaf模板与freemaker
SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...
- web开发之Servlet 三
昨天我们学习了Servlet的运行过程和生命周期,那么今天我们学习下Servlet中非常重要的两个类:ServletConfig ServletContext 我们可以看到,与顶层Servlet主动 ...
- python web开发之flask框架学习(2) 加载模版
上次学习了flask的helloword项目的创建,这次来学习flask项目的模版加载: 第一步:创建一个flask项目 第二步:在项目目录的templates文件夹下创建一个html文件 第三步: ...
- python web开发之flask框架学习(1) 创建flask项目
python 开发越来越火,作为菜鸟,也应该学习一下,感觉还可以,记录下来,方便学习参考. 不管做什么开发首先肯定是安装环境,这里我用的是pycharm,python3.如果不清楚怎么安装环境可以去网 ...
- HTML5+JS手机web开发之jQuery Mobile初涉
一.起始之语 我一直都是在PC上折腾网页的,这会儿怎么风向周边捣鼓起手机网页开发呢?原因是公司原先使用Java开发的产品,耗了不少人力财力,但是最后的效果却不怎么好.因为,Android系统一套东西, ...
- .NET Web开发之.NET MVC框架
摘要:MVC是一种架构设计模式,该模式主要应用于图形化用户界面(GUI)应用程序.那么什么是MVC?MVC由三部分组成:Model(模型).View(视图)及Controller(控制器). MVC概 ...
随机推荐
- objccn-iOS上的相机捕捉
在第一台iPhone时,在app里面整合相机的唯一方法就是使用UIImagePickerController.到了iOS4,发布了更灵活的AVFoundation框架. UIImagePickerCo ...
- [转] ImageView的android:adjustViewBounds属性
原文链接:http://blog.csdn.net/pingchuanyang/article/details/9252689 取值为true时: Adjust the ImageView's b ...
- js: 从setTimeout说事件循环模型
一.从setTimeout说起 setTimeout()方法不是ecmascript规范定义的内容,而是属于BOM提供的功能.查看w3school对setTimeout()方法的定义,setTimeo ...
- spring集成常用技术的配置
使用spring集成其他技术,最基本的配置都是模板化的,比如配置视图模板引擎.数据库连接池.orm框架.缓存服务.邮件服务.rpc调用等,以spring的xml配置为例,我将这些配置过程整理出来,并不 ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- iOS之九宫格图片
照片 现在人们的生活越来越丰富了,很多美好的瞬间都定格在一张张色彩绚丽的照片上,或许把照片珍藏在相册里,或许通过社交软件分享给亲朋好友.那社交软件上的照片是以什么形式展现的呢?那么接下来就要说到九宫格 ...
- 为 placeholder 自定义样式
textarea::-webkit-input-placeholder{ padding: 1em; } textarea::-moz-placeholder{ padding: 1em; } 同理, ...
- 修改PHP 加载loaded configuration file 目录
在/usr/local/php/etc/目录下新建php2.ini 复制php.ini 内容到php2.ini 或cp php.ini 也行. 配置php-fpm 参数: Usage: php [-n ...
- C语言中的强符号与弱符号
转自:http://blog.csdn.net/astrotycoon/article/details/8008629 一.概述 在C语言中,函数和初始化的全局变量(包括显示初始化为0)是强符号,未初 ...
- [.NET]二维码生成
又好一段时间没有写写东西了,继续回归原来的模式,多做记录,最近要实现个unity的二维码方面的功能,首先就要解决生成二维码的问题,这个倒是有这方面的组件,然后我通过强大的反编译工具Reflector( ...