The Personal Blog of Todd Sharp

Spark Java Views With Apache FreeMarker

Posted By: Todd Sharp on 4/7/2017 7:05 GMT
Tagged: Apache FreeMarker, Groovy, Java, Spark Java

In the last few posts I took a look at using Thymeleaf for view rendering and templating with Spark Java.  Thymeleaf has it's advantages and disadvantages, but I could see using it in an application without suffering too much grief and having it actually be enjoyable to work with.  I thought I'd take a look at another option for views in Spark Java:  Apache FreeMarker. FreeMarker has been around a looooongggg time - since 1999.  Amazingly, it's still under active development (the most recent release was 3/25/17).  Needless to say - it's not legacy, it's aged like a fine wine.

So can it handle view rendering and templating as well as Thymeleaf?  Let's give it a shot.  As with everything, we need to declare our dependency (no, we're not still talking about wine here):

compile group: 'com.sparkjava', name: 'spark-template-freemarker', version: '2.5.5'

Next we'll add a new route in the Broadcast class just as before:

Our view files with FreeMarker go in src/main/groovy/resources/spark/template/freemarker.  Let's look at a SiteMesh equivalent example since that's what we looked at last with Thymeleaf.  FreeMarker calls their layout templates "macros", so let's create a simple layout template that we'll then apply to our child view.

A few things worth nothing with the macro file above.  Starting with line 1:

<#macro mainLayout title="Freemarker Layout Template" menu="">

The first attribute - mainLayout - is how we'll later refer to this macro from the child view.  The second and third attributes - title and menu - are variables we can pass in from the child.  Title has a default of "Freemarker Layout Template" which will be initialized if nothing is received from the child, and menu has an empty default (it'll be null if empty).

These attributes are now available for our use using the familiar ${} notation.  See line 13, for example, where we use the ${title} attribute and line 42-44 where we loop over the menu List element to create a dynamic menu with a list passed from the model in the child view.  This is the type of functionality that I'd like to see in Thymeleaf - simple passing of model variables from the child to the layout for dynamic bits.  

The last item worth mentioning is on line 53:  the <#nested/> tag.  This will be replaced with the contents from the child caller.  Speaking of children:

Ugh...sorry.  The child template:

The child simply imports and applies the layout - passing variables as needed.  Again, any model values can be rendered using the ${} token format.  And that's it, dynamic content in a reusable format.  It doesn't stop there though - like Thymeleaf there is an entire framework available for use.  Directives, expressions, interpolations - formatting (dates, numbers, booleans, etc), conditionals (if/else, switch/case) - anything you could possibly need inside a view is available.  My only gripe is the lack of IDE support with IntelliJ IDEA Community Edition, but that's yet another reason why I need to shell out for a license.

Image by Julius_Silver from Pixabay

Related Posts

Authenticating Users with Oracle IDCS via OpenId Connect and Micronaut

Authenticating Users with Oracle IDCS via OpenId Connect and Micronaut

Security is the most important aspect of your web application. Fortunately, there are tools available to help make life much easier when dealing with things...

Adding Tracing to Your Distributed Cloud Native Microservices

Adding Tracing to Your Distributed Cloud Native Microservices

When adopting cloud-native technologies and certain architectures such as the microservice pattern, observability and monitoring become a huge need and...

Project GreenThumb Part 5 - The Front-End, Build Pipeline, Push Notifications and Overall Progress

Project GreenThumb Part 5 - The Front-End, Build Pipeline, Push Notifications and Overall Progress

In this short blog series, I introduced you to Project GreenThumb, a project that I created to automate and monitor the process of growing seedlings with...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!