Search in sources :

Example 1 with FreeMarkerTemplateEngine

use of io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    // To simplify the development of the web components we use a Router to route all HTTP requests
    // to organize our code in a reusable way.
    final Router router = Router.router(vertx);
    // In order to use a template we first need to create an engine
    final FreeMarkerTemplateEngine engine = FreeMarkerTemplateEngine.create(vertx);
    // Entry point to the application, this will render a custom template.
    router.get().handler(ctx -> {
        // we define a hardcoded title for our application
        JsonObject data = new JsonObject().put("name", "Vert.x Web").put("path", ctx.request().path());
        // and now delegate to the engine to render it.
        engine.render(data, "templates/index.ftl", res -> {
            if (res.succeeded()) {
                ctx.response().end(res.result());
            } else {
                ctx.fail(res.cause());
            }
        });
    });
    // start a HTTP web server on port 8080
    vertx.createHttpServer().requestHandler(router).listen(8080);
}
Also used : FreeMarkerTemplateEngine(io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)1 Router (io.vertx.ext.web.Router)1 FreeMarkerTemplateEngine (io.vertx.ext.web.templ.freemarker.FreeMarkerTemplateEngine)1