recursive.codes

recursive.codes

recursive.codes


The Personal Blog of Todd Sharp

Spark Java Views Using Thymeleaf

Posted By: Todd Sharp on 4/5/2017 12:50 GMT
Tagged: Groovy, Spark Java, Thymeleaf

The next step in using Spark Java with Groovy that I would like to look at is getting data into a view.  With Grails, we're used to using GSP pages - and they work great, but Spark Java doesn't have view support out of the box.  Instead, it let's you choose a template engine.  The alternative is to simply serve static HTML pages and retrieve any data via Ajax calls.  That's a valid strategy in some cases, but most sites will require some level of dynamic data in the view and requiring Ajax for all of that data isn't always the best solution.  Enter Thymeleaf.  In their words:

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text.

The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates. To achieve this, it builds on the concept of Natural Templates to inject its logic into template files in a way that doesn’t affect the template from being used as a design prototype. This improves communication of design and bridges the gap between design and development teams.

Thymeleaf has also been designed from the beginning with Web Standards in mind – especially HTML5 – allowing you to create fully validating templates if that is a need for you.

At it's simplest, Thymeleaf will allow us to pass a model into our view and render a value in the view via a simple syntax.  Here's a modified Bootstrap class from the one we created in the last blog post:

We declare our engine on line 8, and in our "/thymeleaf" route we use the engine to return our rendered view, passing the model as the first argument and the view name (sans the extension) as the second argument.  

Let's create that view now.  Create a directory (if none exists) at src/main/groovy/resources/templates and create a file called thymeleaf.html.  Here's what that view looks like:

We shouldn't forget to add our dependency for Thymeleaf in our build.gradle script:

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

Now run the app like before with gradle runServer.  Hit the view in your browser and you'll see:

We've just scratched the surface of what Thymeleaf can do.  This tutorial has many more examples of the various features, but the syntax has a "GSP like" feel to it which I like.  I should note that Spark Java supports many different template engines so you're not locked into using Thymeleaf.  Feel free to use any that you're comfortable with.

Image by smarko from Pixabay



Related Posts

Simulating Texas Hold'em With Groovy

Simulating Texas Hold'em With Groovy

Last week I had a bit of free time and decided to see how difficult it would be to write a Texas Hold'em poker simulation in Groovy.  My goal wasn't...

Intro To Oracle Application Container Cloud Services

Intro To Oracle Application Container Cloud Services

I've long been an AWS user, but recently one of my projects at work has expressed some interest in getting our platform running on multiple cloud service...

Creating A Slack Clone With Twilio, Angular 5 And Electron

Creating A Slack Clone With Twilio, Angular 5 And Electron

I've heard plenty about Twilio over the past few years and had always wanted to learn more about their APIs.  They have a ton of different products...

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!