Java HTML Templating With Handlebars and Undertow

Researching how to create a website in Java can be a daunting task. A few Google searches and you end up with tons of libraries and strong opinions. JSP, JSF, Spring MVC, GWT, Vaadin and many more. All of them seem like they have quite a learning curve. All we really need is to pass an object to a template and render some HTML, nothing fancy. Let’s take this a step further and follow the node/express approach of simply plugging in an HTML templating engine. Enter Handlebars – a perfect, fairly simple syntax added to plain HTML anyone should be able to pick up immediately (Yes, MVC frameworks can use this also, but do we need a framework for what a single class can do?). It also conveniently has ports in many languages. This means we can use the same templates client-side (JavaScript) and server-side if we want.
HTML Templating Utility
This is a rare case where we decided to make a simple abstraction hiding the underlying jknack handlebars implementation. Since it only really needs a few methods, we can hide the implementation and easily swap it out later. Notice how we have a few config options. When we are running locally, we want handlebars templates to be compiled on the fly and NOT cached. We also utilize HTML compression for all of the HTML-specific methods. We also offer some non-HTML templating methods. Sometimes it might make sense to abuse an HTML templating library to solve a similar problem.

Link: https://dzone.com/articles/java-html-templating-with-handlebars-and-undertow?utm_medium=feed&utm_source=feedpress.me&utm_campaign=Feed%3A+dzone%2Fwebdev