Spark Java Views Using Thymeleaf - SiteMesh Like Layouts

In the last post we looked at how Thymeleaf handles reusable layouts.  A reddit user enlightened me about an open source ‘dialect’ for Thymeleaf that makes it behave in a similar manner to SiteMesh.  It’s documented pretty well, but...

https://objectstorage.us-ashburn-1.oraclecloud.com/n/idatzojkinhi/b/img.recursive.codes/o/clooney.gif

Yeah, documentation can be boring sometimes, so I thought I’d put together a quick post on how that would look in the Spark Java application I’ve been working with.  It works well, but it suffers from an inability to pass model variables to the layout template.  That’s something that SiteMesh can handle (although in a less-than-elegant manner in my opinion) and in my view it is almost deal breaker.  Something as simple as a nav menu that is common across all pages will ultimately require some dynamic data, and if we’re going to need to th:insert the fragment on every page, then what are we really gaining by using this style of layout template?  I’ll add how I’d think this could be handled at the end of this post, but first let’s look at how to implement this dialect in the project.

Thymeleaf - Passing Variables From Child To Layout

Just a quick update after my last post to clarify things a bit.  I mentioned in that post my dismay regarding the inability to pass model variables from the child view to the parent layout - and I shared that concern with the dialect developer Emanuel on GitHub.  He quickly wrote back to clarify that it is indeed possible:

Hi Todd! First of all, nice blog - I came across it the other day when you wrote about using Thymeleaf w/ Spark \

Spark Java Views Using 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:

Spark Java Views Using Thymeleaf - Layouts

In the last post we looked at plugging in Thymeleaf into a Spark Java application for view rendering.  The concept was pretty simple: using the Thymeleaf engine, render a view with a map of variables to use as the model.  But in reality, our applications need a bit more complexity.  They need reusable layouts.  In this post we’ll take a look at how to handle that with Thymeleaf.  

Reusable layouts include things like headers, footers, scripts and other things like nav menus that are common across the application.  Thymeleaf accommodates these by using what they call “fragments” - reusable blocks of code defined by the th:fragment attribute that can be called from your templates using the th:replace, th:insert or th:include attributes.  You can read all about it in their docs, but let’s take a look at a practical example below.

Getting Started With The Spark Java Framework

I published a post last week showing how to use Grails to create a website on the Raspberry Pi.  After some feedback and conversations about whether this was “overkill” for a simple Raspberry Pi website I decided to revisit the topic and see how I could simplify things a bit without sacrificing the power of Groovy and Pi4J. I’ve done a bit of digging and discovered the Spark Java framework.

Remote Deployment For IntelliJ IDEA Community Edition

I’ve been using IntelliJ IDEA Community Edition on my personal machine to work with some of the demos that I’ve been using for my series on using Groovy to program on the Raspberry Pi.  One of the features I’ve missed most from Ultimate Edition is the ability to remotely deploy to the Pi to keep the code in sync.  I’ve worked around it by using SCP every time I change something, but it’s a bit tedious to do that every time, so I sought out additional options and found the Source Sync plugin.  It’s pretty easy to install and so far I’ve found that it works pretty much identically to the Remote Deploy feature in Ultimate Edition (with the exception that it doesn’t provide feedback that it is syncing).  I’ll use this plugin going forward so if you plan on following along with the series it would be a good idea to install it!
\

Brain Teaser: Find The Missing Number

When I first saw this challenge I thought it would be a lot more difficult than it turned out to be.  Here is the challenge:

Here’s a list with numbers from 1-250 in random order, but it’s missing one number. How will you find the missed number?

https://objectstorage.us-ashburn-1.oraclecloud.com/n/idatzojkinhi/b/img.recursive.codes/o/wouldnt-say-ive-been-missing-it.jpg

I generated the list of numbers on the fly each time:

// first, create the list 
List listWithNumbersInRandomOrder = (1..250).toList()

// randomize it
Collections.shuffle(listWithNumbersInRandomOrder, new Random(System.nanoTime()))

// remove 1 of the items
Integer removed = listWithNumbersInRandomOrder.remove(0)

// have we removed one?
assert listWithNumbersInRandomOrder.size() == 249

But if you’d rather work from a static set, here’s a list for you:

A Closer Look At Sorting Algorithms

As I mentioned in a previous post, sorting algorithms typically play a large role in programming interviews.  Those who follow the traditional path into the programming world and obtain a CIS degree are typically exposed to algorithms.  Those among us who follow a less traditional path into this world are less familiar with them.  

https://objectstorage.us-ashburn-1.oraclecloud.com/n/idatzojkinhi/b/img.recursive.codes/o/no-idea.jpg\

I decided to take a deeper dive into sorting algorithms and implement some of them to see: