Vert.x with a template engine

1,729 views
Skip to first unread message

Matty Southall

unread,
Mar 8, 2016, 11:43:04 AM3/8/16
to vert.x
So I was attempting to implement a template engine into Vert.x by looking at the examples posted here, "https://github.com/vert-x3/vertx-examples/blob/master/web-examples/src/main/java/io/vertx/example/web/templating/thymeleaf/Server.java", but for some reason when I visit a route I get a NullPointerException, to me it seems like it can't find my template files.

I have tried placing my template files within src/main/java/templates and also /src/main/resources/templates and I can't get them to pick up correctly, I specify the template name in the route has "templates/index.html" for example, the index.html file also does exist.

Can anyone assist me?

Also on a side note, it would be great to upgrade the Thymeleaf templating library to Thymeleaf 3.0, even though it is still in beta, support for it as it is now would be great!

ad...@cs.miami.edu

unread,
Mar 8, 2016, 3:48:35 PM3/8/16
to vert.x
Hi,

I have never used Thymleaf, but I have started using FreeMarker (another template engine) with Vert.x.  If you are interested in getting FreeMarker to work with vertx, let me know and I can provide a tutorial on it (perhaps in a few days). 

But really, using FreeMarker with vert.x is just like using FreeMaker in normal java - you just have to be aware that freemarker may block the eventloop for a short period while doing io (loading template from file or DB) and cpu for the processing.  So, I wrap in all in a worker verticle. 

I imagine that Themleaf would be pretty much the same, but I have never used it so I can't be sure.

Paulo Lopes

unread,
Mar 9, 2016, 3:08:16 AM3/9/16
to vert.x
FYI: for 3.3 we are including 2 new engines:

* freemarker
* peeble

See the list here:

https://github.com/vert-x3/vertx-web/tree/master/vertx-template-engines

Cheers,
Paulo

Matty Southall

unread,
Mar 9, 2016, 5:44:19 AM3/9/16
to vert.x
An example would be of great use.

Matty Southall

unread,
Mar 9, 2016, 5:44:40 AM3/9/16
to vert.x
Thymeleaf 3.0 would be a great addition too!

Paulo Lopes

unread,
Mar 9, 2016, 5:53:55 AM3/9/16
to vert.x

Paulo Lopes

unread,
Mar 9, 2016, 5:55:22 AM3/9/16
to vert.x
We accept pull requests :) We have an 2.x implementation if you feel confident enough maybe you could see how to upgrade to 3.x?

Matty Southall

unread,
Mar 9, 2016, 5:56:33 AM3/9/16
to vert.x
I had a look at those examples, and I was getting a null pointer exception with "engine.render(ctx, "templates/index.html"" I don't think it can find my templates correctly. Maybe I am doing my dependencies wrong and have Vertx 3.2 and require 3.3 snapshot?

Paulo Lopes

unread,
Mar 9, 2016, 6:03:18 AM3/9/16
to vert.x
What engine are you trying to use?

remember that freemarker and pebble are only available on the snapshot release, so you need to add the sonatype snapshot reppo to your maven and/or build the vertx-web project from git locally.

what is the filename of your template, the engines try to load the name you pass + default template extension, so for example in your case if you were using the freemarker engine you should have a file "templates/index.html.ftl" if you specify the expected template extension then the engine is "smart" and does not append the extension.

Matty Southall

unread,
Mar 9, 2016, 6:08:47 AM3/9/16
to vert.x
Thymeleaf 2 engine.

I passed "templates/index.html" and had my templates in src/main/resources/templates/index.html, but I also tried it in various different locations and couldn't get it to load. So it could be appending it as index.html.html?

Also I'll take a look at Thymeleaf 3, its still in beta but would be nice to get an implementation going.

Paulo Lopes

unread,
Mar 9, 2016, 7:26:38 AM3/9/16
to vert.x
thymeleaf default extension is .html so it should work, can you show your code? maybe there is something else going on.

Matty Southall

unread,
Mar 9, 2016, 2:59:28 PM3/9/16
to vert.x
The code is:

@Component
public class ServerVerticle extends AbstractVerticle {

    final ThymeleafTemplateEngine engine = ThymeleafTemplateEngine.create();

    final Router router = Router.router(vertx);

    @Autowired
    ServerConfiguration serverConf;

    @Override
    public void start() throws Exception {
        engine.setMode("LEGACYHTML5");

        router.route().handler(CookieHandler.create());
        router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));

        router.route(HttpMethod.GET, "/").handler(handler -> {
            engine.render(handler, "templates/index.html", res -> {
                if (res.succeeded()) {
                    handler.response().end(res.result());
                } else {
                    handler.fail(res.cause());
                }
            });
        });

        vertx.createHttpServer().requestHandler(router::accept).listen(serverConf.getPort());
    }
}

I am using Spring and the main method is simply:

@SpringBootApplication
public class WebApplicationMain {

    @Autowired
    private ServerVerticle serverVerticle;

    public static void main(String[] args) {
        new SpringApplicationBuilder(WebApplicationMain.class)
                .bannerMode(Banner.Mode.OFF)
                .run(args);
    }

    @PostConstruct
    public void after() {
        Vertx vertx = Vertx.vertx();

        vertx.deployVerticle(serverVerticle);
    }
}

I put my templates in "src/main/resources/templates". Using LEGACYHTML5 too for Thymeleaf I have the required dependency "nekohtml" setup too.

Jez P

unread,
Mar 9, 2016, 3:32:08 PM3/9/16
to vert.x
Don't know about Thymeleaf but the demo I put together for vertx-pac4j uses the HandlebarsTemplateEngine just fine, I'd expect Thymeleaf's setup to be similar 

Jez P

unread,
Mar 9, 2016, 3:34:20 PM3/9/16
to vert.x
Are you sure your SpringBootApplication isn't deploying a listener on the server port you're configuring, in which case your vertx http server won't be able to bind to it (in which case nothing will hit your vertx handler). Have you tried making your server verticle just write back something trivial without using templating to ensure your requests get to it?

Matty Southall

unread,
Mar 9, 2016, 3:46:30 PM3/9/16
to vert.x
Spring isn't running anything on any port, I am not using any of its web components. I have tried again and get an error of "[20:44:09] ERROR org.thymeleaf.TemplateEngine - [THYMELEAF][vert.x-eventloop-thread-1] Exception processing template "templates/index.html": java.lang.NullPointerException" and also mentions "Caused by: java.lang.NullPointerException: null
at io.vertx.ext.web.impl.Utils.readFileToString(Utils.java:163)" in the stack trace.

I assume it can't find the template file for some odd reason, no matter where I place it.

Matty Southall

unread,
Mar 11, 2016, 7:17:51 AM3/11/16
to vert.x
I have fixed my null issues and it was an issue with defining the Router and TemplateEngine outside of the start() method.

Also I have Thymeleaf 3 working, I have forked the repository and i'll make a Thymeleaf3 project (as Thymeleaf 3 is still also in beta) and do a pull request.
Reply all
Reply to author
Forward
0 new messages