A Mustache MVC Example App with Bootique

Bootique supports a number of modules that integrate technologies like JCache, JDBC, Jersey REST and RabbitMQ. See the complete list of Bootique modules here. Bootique integrates Jersey MVC and the Mustache Templating Engine in bootique-mvc. While most of the Bootique modules include example code, bootique-mvc currently does not. I thought I'd write one for my own benefit and for anyone else interested in Bootique. You'll find it on GitHub.

In this blog post we're going to look at some of the highlights of Bootique and its MVC and Mustache implementation. First, here's the exciting output of http://localhost:9000. "Hello, Bob McCall!"

The project is laid-out like your typical MVC app, Controller, Model and Views.

Launching Bootique

Before looking into the MVC-Mustache logic let's look at how we launch a Bootique Application. In our main() class we're firing up an integrated Jetty Server so we add a --server argument and set the path to our bootique.yml configuration file.

We specify Jetty properties and other application settings in our bootique.yml file.

The Google Guice Factor

Bootique uses Google Guice in its core architecture which you see in the Launcher class. The Module being implemented is Google Guice.

The Google Guice Module configure() override is where we'll setup our Bootique resources.

MVC and Mustache

The implementation of Mustache with MVC uses View classes to populate a Mustache template with a model. Here is the Controller method populating home.mustache with a User object model.

Here is our very simple PageView class. We could construct views to be reusable or as complicated and view-specific as required.

We don't have to use the AbstractView approach. We can use any template resolver approach we like. One caveat of AbstractView is that all templates must be in the same package as the View.

Testing with Bootique

Every Bootique module has testing support specific to the module and the technologies integrated in that module. In our MVC module we need to test with Jetty so we have a JettyTestFactory Rule for supporting a Jetty test environment.

We'll launch our test runtime with the configuration we choose and bind test resources like we did in our app's Launcher class earlier. Notice the http://localhost:9001 url. We configured that in our bootique-tests.yml just as we configured it in our app's bootique.yml.

Source code for this post can be found in my bootique-mvc-demo app on GitHub.

Posted July 14, 2017 08:52 AM EDT

More Like This Post