MoreLikeThis Post Display with Spring MVC, Thymeleaf and FreeMarker

We covered the Solr backend of our MoreLikeThis Post display (keywords, More Like This, MLT, You May Also Like…), so we'll finish with displaying those posts using Spring MVC, Thymeleaf and FreeMarker. We'll even throw in a couple of JUnit tests.

First our MoreLikeThis Display on the page. (The Post was “Profile Image Uploads: Intro”.)

The Pieces

Here are the main pieces of our MoreLikeThis Display feature:

  1. A Spring MVC Controller Method displays the post and adds any MoreLikeThis-specific variables. When those variables are provided Thymeleaf displays the morelikethis fragment.
  2. A Spring MVC Rest Controller serves the MoreLikeThis HTML, retrieved with JQuery $.get(/url) from the Thymeleaf fragment.
  3. Two ApplicationSetting properties determine if MoreLikeThis Posts are to be displayed and, if so, how many posts to display
  4. Individual MoreLikeThis posts are displayed with FreeMarker templates
  5. If the blog is new and the number of MoreLikeThis posts exceeds the number of available posts, a “Coming Soon…” HTML message is displayed generated by a FreeMarker template

Post Controller

In our Post Controller displaying the Permalink Post Page we pass along MoreLikeThis variables to be used by Thymeleaf (1).

Post Rest Controller

We'll jump to our Post Rest Controller which you see serves up HTML and not JSON.

Thymeleaf and the Post Rest Controller

Thymeleaf will render the MoreLikeThis fragment based on the moreLikeThisDisplay variable in the Post Controller we saw earlier.

With Thymeleaf's JavaScript inline variables support we'll be able to pass the postId in our JQuery $.get() function and replace our mlts tag with MoreLikeThis posts.

FreeMarker

Here's a FreeMarker MoreLikeThis template.

And this is the method used to populate our posts, or if encountering an IndexOutOfBounds Exception for a new blog with insufficient posts, a “Coming soon…” FreeMarker template is retrieved.

The Tests

Our Tests will provide us with a summary of the action. We have three tests, one to know what happens when 1) the site ApplicationSetting is set to not display MoreLikeThis posts, 2) when new blogs display a “Coming soon…” message, and 3) when everything works as it should.

The test to see what happens for new blogs is probably the most interesting. We want to display MoreLikeThis posts as set in the Permalink get(“/post/{postname}”) Controller Method variables. Thymeleaf displays our fragment and configures it to retrieve the posts as requested. But as we saw above, populateMoreLikeThis() can throw an IndexOutOfBounds exception if requested MoreLikeThis posts is greater than the number of available posts. So our “Coming Soon…” message containing <div class=”nomlt-message”> is in our response content.

The Coming Soon message

For completeness here's a demo of what that “Coming soon…” message looks like.

Source Code Notes for this Post

Source code discussed in this post can be found in my NixMash Blog project located on GitHub.

Posted March 01, 2017 10:34 AM EST

More Like This Post